Entity Framework and DevExpress ASP.NET

Don Wibier's Blog
06 July 2015

Last week Mehul and I did a webinar on Entity Framework and DevExpress.ASP.NET.

Intro

What is Entity Framework?

Entity Framework is an Object Relational Mapping (ORM) tool, much like our own Xpress Persistent Objects (XPO).

An ORM is a layer between the data storage, and your business logic which allows you to manipulate and query your data store by using C# objects instead of working with SqlConnections, SqlCommands, Datareaders and SQL statements!

It means that you have C# classes which represent all entities (tables) in your database and by decorating classes and properties inside those classes, you can specify things like table names, fieldnames, indexes and relationships in your data.

Under the hood of the ORM tool, the appropriate SQL commands are being created runtime, which also means that if you want to change to a different database backend, if the ORM supports it, you can do that without changing any of your C# code! A good example of this is our own XPO, which supports more than a dozen different database back ends by only changing the connection string.

Why Entity Framework?

With the introduction of MVC, it became clear that the use of an ORM together with the MVC design pattern is an excellent combination since you are already working with models (objects) in MVC, and you are working with objects again to get and store information in the database.

Because of this, Microsoft has been putting some great effort in expanding and optimizing the earlier versions of Entity Framework to what it is today (and will become in the future) and they have even added support for WebForms applications as well by delivering an EntityDataSource to the WebForms Toolbox.

Because it is clear that Entity Framework is the Microsoft way to go, we have already changed over all of our demo applications from Sql Data readers to EntityFramework in v15.1.

This gives you the opportunity to see what is involved in start using an ORM.

What did we cover in the Webinar?

Mehul showed you a couple of different methods on how to start with Entity Framework:

Code first

This means that you will create your data-model directly in C# classes and when you start the application, the ORM will sync the database structure with the C# class hierarchy. This is the strategy Microsoft advices.

Database first

In this scenario, the database is modeled and there will be some synchronization design-time to keep your class hierarchy up to date with the database structure

Model first

This approach will open a designer in Visual Studio which allows you to design your database, where when finished, the C# classes will be created / updated as well as the database structure.

Demonstrated approaches

I have showed you 3 approaches on how to access the data from the Chinook Sample Database, by using the Code First approach. Inside Visual Studio there is a feature which allows you to start a Code First project by existing database. This wizard will analyze the database structure and create the Code First classes for you.

I have used the Chinook Demo Database for this which is freely available on CodePlex at: https://chinookdatabase.codeplex.com/

Microsoft Entity DataSource

The first demo I did was by using the Microsoft Entity Datasource control. It is important to get an updated version from NuGet since the version installed in your toolbox is for Entity Framework v4.5 where the default version as of writing is v6.x.

NuGetEFDatasource

This version has limited design-time support and you need to type the DB Context class and the desired entity property manually.

The Microsoft Entity Datasource was setup quickly and also supports standard CRUD operations out of the box which I showed with our DevExpress ASP.NET GridView control including standard CRUD operations.

DevExpress EntityServerModeDataSource

Since the sample database does not have mega much data, I am talking about millions of records, the Entity Datasource control performs its task quickly. This will change though when there is a huge database involved and you want to enable the DevExpress ASP.NET GridView’s sorting, grouping, paging, filtering etc.

You can see the impact on our demo site at: http://demos.devexpress.com/ASPxGridViewDemos/DataBinding/LinqDataSourceServerMode.aspx

For such usage, we have delivered the DevExpress EntityServerModeDataSource control. It allows you to perform the required tasks on large datasets much quicker because this control determines what kind of data store is behind the your ORM model and fires the most optimal SQL statements. We have taken some of our XPO expertise and put it in this control.

Also our visual controls like the GridView, DataView and CardView know if they are connected to our EntityServerMode DataSource, and will communicate slightly different with it which results in a dramatic performance increase!

Since this control’s main meaning of live is data reading, you will need to code a number of events manually to allow standard CRUD operations.

Standard ObjectDataSource

While this control was initially released with ASP.NET v2, it is still one of my favorite controls when doing WebForms and data access.

It allows you to totally separate the business logic from the presentation layer and you also have total control on what data to fetch when you enable certain properties on the control.

This control needs a class which implements a number of methods for Selecting and optionally for Updating, Inserting and Deleting.

I have shown you that you can use LINQ queries inside the Select method to fetch results from the datastore. Entity Framework will transform these LINQ queries to SQL statements under the hood.

Conclusion

In case you haven’t checked an ORM like Entity Framework out, we would urge you to take a look at it since it helps you in developing secure and modern applications. We have shown you that it is not MVC proprietary technology and that our DevExpress data controls can take full advantage of it.

You can download the demo project I have been building, as usual, from my GitHub account at: https://github.com/donwibier/DXWebFormsEF.

In the project on GitHub, you will notice that I did implement the CRUD operations, and some additional powerful methods in the ObjectDataSource class for real paged data fetching with LINQ, in combination with Sorting and Filtering.

Let me know what you have already done with Entity Framework or if you’re facing any difficulties!

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.