clip_image002Microsoft released the updated Entity Framework 6 (EF6) framework in early 2013 to eager developers who wanted to use the newly open sourced data access library. This was a huge step for Microsoft, they were now developing and delivering their core data access technology from an open source repository for all to see. Julie Lerman called this the Ninja Edition of the library, indicating that it performed very well and scaled well. However, the one thing that developers saw that they didn’t like was a lack of support for the Entity Framework Data Source in ASP.NET.

With a fresh install of Visual Studio 2013, it was impossible to use Entity Framework 6 with ASP.NET web forms to easily build data enabled web pages. This sent developers running to their previous version of Entity Framework so that they could have support for this model of rapid application development.

In late February 2014, Microsoft heard the community and released a preview release of an Entity Framework data source control for ASP.NET. With this control, you could once again drag a grid onto a design surface and easily connect it to your database for all CRUD database interactions.

With this publication from Microsoft, we at Telerik responded. We announced compatibility with this new Entity Framework data source starting with our Q1 2014 release of the UI for ASP.NET AJAX. Let me show you how to get started with using these two resources together.

Add Entity Framework to your project

Entity Framework is now completely isolated into a collection of Nuget packages that you can add to your project. For the ASP.NET data source, you should install the Microsoft Asp.NET EntityDataSource from Nuget with a command in the Package Manager console like:

Install-Package Microsoft.AspNet.EntityDataSource -pre

This package will not just add the control to your project references, but will also add a special line to your web.config file that will allow you to switch and use the new EntityFrameworkDataSource with an ef namespace:

   <add tagPrefix="ef" assembly="Microsoft.AspNet.EntityDataSource" namespace="Microsoft.AspNet.EntityDataSource" />

Listing 1- Changes to web.config

Configure Your Page

With that small change and an existing EF6 entity data model in your project, you can easily use the data source just as you used the previous version. Consider the following code to connect my Products table from a data context called GameShopEntities:

<ef:EntityDataSource ID="myData" runat="server" ContextTypeName="WebApplication35.Models.GameShopEntities" EntitySetName="Products"> </ef:EntityDataSource> 

Listing 2 - Entity Data Source in use

I can connect this just as I had before to any Telerik control for ASP.NET, using the DataSourceID attribute to instantly wire up data retrieval and update interactions. In the following snippet, I wrote a few lines of markup and pointed the DataSourceID attribute appropriately to the data source object.

<telerik:RadGrid runat="server" ID="grid" DataSourceID="myData"> </telerik:RadGrid> 

Listing 3 - Simple Reference with DataSourceID

Our grid looks great, and contains data without having to write any SQL statements, databind commands or other C# code. I can complete a simple page like this without leaving my markup editor.


Figure 1- Simple Grid Fetching Data from Entity Framework


Again and again, Telerik is there for our customers. We listen to your feedback, and we build what you need to be successful. As soon as Microsoft made this capability of Entity Framework available to us, we turned it around for you as well. Once we have a confirmed final version of this version of the Entity Framework library from Microsoft, look for us to show more samples using EF with Telerik ASP.NET AJAX controls.

About the Author

Jeffrey T. Fritz

Jeffrey T. Fritz is a Microsoft MVP in ASP.Net and an ASPInsider with more than a decade of experience writing and delivering large scale multi-tenant web applications. After building applications with ASP, ASP.NET and now ASP.NET MVC, he is crazy about building web sites for all sizes on any device. You can read more from Jeffrey on his personal blog or on Twitter at @csharpfritz. Google Profile

Related Posts


Comments are disabled in preview mode.