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!

9 comment(s)
Zachary Burns
Zachary Burns

Don - I did a bunch of research between EF and XPO and started writing a rather large application to let an end-user support SQL Server, SQLite, mySQL (over SSH on a Linux server), etc.  

Took me a while, but I'm just concerned that XPO will stop being supported and EF doesn't seem mature enough to handle the non-Microsoft back-ends that I want to support.

Do you still think that XPO is the way to go in this instance, or will EF support what I want to do (even mySQL over SSH, etc).

8 July, 2015
FloridaDevs
FloridaDevs

I am extremely interested to see how DX deals with the release of EF7 as it will be ONLY code first from.  This is a huge pivot that Microsoft is implementing but NOT at the VS2015 release date.

8 July, 2015
Don Wibier (DevExpress)
Don Wibier (DevExpress)

Zachary:

Yes, XPO supports more then a dozen different database backends without changing a single line of C# code.

We are quite proud of XPO and all that it offers. We support it and certainly want to improve it, so if there are suggestions you have, please let us know.

Concerning EF and its future, we are following the development of v7 with great interest but you should ask the EF team at Microsoft if they have plans to support different database backends.

9 July, 2015
Don Wibier (DevExpress)
Don Wibier (DevExpress)

FloridaDevs:

Our controls are data agnostic. This means that e.g. our ASPxGridView really doesn't care what kind of DataSource it is connected to.

For you this means that you can start using EF v7 right now with our Data aware controls.

We are keeping a close look at EF v7 development, and once an official release has been done, we will certainly investigate if changes need to be made to get high performance results with EF v7.

9 July, 2015
developer_paul
developer_paul

@zachary I have a very large application that uses XPO. EF isn't as mature or feature rich as XPO at the moment. If I had to start over now I would still use XPO.

11 July, 2015
Michael Hills
Michael Hills

Thank you so much for verbal description of seminar

I have spent far too much of my time stepping through DX webinars and backtracking trying to reproduce the demos

I have never used Git before so had to do some learning on getting it into VS

When I succeeded and built the project I got a NuGet error

Error 2 NuGet Package restore failed for project DXWebFormsEF.Data: Unable to find version '6.1.3.1' of package 'DX.Data.Extensions'.. 0 0

Please help

thank you

11 July, 2015
Steve Sharkey
Steve Sharkey

XPO totally transformed my productivity when producing customer apps - it is mature enough to be reliable & solid for larger apps and light weight enough to be an easy choice on smaller apps. I to worry when dx get so enthusiastic in promoting EF - I have a large investmnent in XPO. I trust the relatively static feature set is just down to the mature nature of the product - afterall @don's offer to add improvements has me scratching my head to think of any...

13 July, 2015
renejdm
renejdm

I too am wondering about this. XPO looks great but I do not want to start a new project with it if Devexpress is going to scrap it down the road.

14 July, 2015
Minguet
Minguet

I was trying to use XPO with ASP.Net MVC, but there is no support out the box, as explained in this post www.devexpress.com/.../K18525

The combination of all the 50+ controllers along with XPO could have been an excellent combination.

11 April, 2016

Please login or register to post comments.