ASP.NET 4.0 Part 15, Data Enhancements
Welcome to part 15 of my tour through ASP.NET 4.0 [http://blog.hmobius.com/post/2010/02/09/ASPNET-40-Cometh.aspx]. In this episode, we’re going to look at how webforms developers can make more use of both Dynamic Data 4.0 and Entity Framework 4.0 thanks to several new features baked into ASP.NET 4.0.
For those of you unfamiliar with these two products, Entity Framework [http://msdn.microsoft.com/en-gb/data/aa937723.aspx] is the ‘enterprise-level’ data modeller (O/RM) technology, aimed at larger applications where LINQ2SQL was aimed at small ones. Dynamic Data [http://www.asp.net/dynamicdata/] meanwhile is a rapid development tool used to create a basic scaffolding for a website based on the schema of the database that supports. Almost a CSS-like construct based on the type of data in a field rather than the type,class or id of an HTML element.
It’s odd how both Dynamic Data (DynData) and the Entity Framework (EF) were both released in .NET 3.5 SP1 almost as a tester to garner public reaction to Microsoft’s efforts thus far with both projects. And it’s fair to say that mistakes / omissions were made in those v1 releases which are now being corrected. EF was vilified in comparison to other, more mature OR/M frameworks for many reasons including
- Short list of shortcomings that have been fixed in EF 4 * Unit testing shortfalls. EF objects couldn’t be generated as plain old CLR objects (POCOs) - they had to be classes derived from EntityObject which meant they would hit the db when tests were run against them. * EF didn’t support many to many relationships
DynData, which sat on top of EF, suffered from both EF’s shortcomings and some of its own including.
- DynData 3.5 required you to start a new project and coudn’t easily be integrated into a webforms or MVC application * If you used EF as the data model, DynData couldn’t tell which field in a database table was the primary key and would model it as an editable field. You had to go into the model and annotate it separately. * Scaffolding templates for only basic field types and none for entities (types defined across several table joins)
Needless to say, both DynData 4.0 and EF 4.0 address these issues and many more. It’s now fair to say that both technologies are well beyond where their first versions perceptibly fell short.
EnableDynamicData One of the goals of ASP.NET 4.0 was to make DynData’s scaffolding available to both Webforms and MVC. As noted earlier, DynData v1 was a project of its own that couldn’t be easily retrofitted into an existing website, be it using Webforms or MVC. To remedy this, ASP.NET 4.0 now includes a new method called EnableDynamicData that you can use on any page in a website or web application.
Consider the scenario where you’ve a FormView or GridView allowing you to update, add to or delete records from a database. Typically, the controls auto-generate columns containing textboxes and maybe a checkbox. Users can add in invalid text, dates, values outside desired ranges etc. So you disable auto-generation and template the columns yourself with a calendar control for dates, say, and validation controls. And then you do the same thing for various other data-bound controls based on the same tables of data. Rather than setting all columns and rows yourself on each control, the EnableDynamicData method tells ASP.NET to generate those customized input and validation controls itself using the DynData scaffolding generated against the database and annotations you may give it as a (central) template.
Let’s take an example.
- Open VS2010 and create a new, empty web site. * If you’re using SQL Express, add an App_Data folder and add in the AdventureWorksLT database. You’ll find it for download on codeplex. * Add add an Entity Data Model to your site. Generate it from the tables in the newly added database. * Add a new page to your site and place on it an EntityDataSource and a GridView. The EntityDataSource should retrieve the EntitySet for the Products table. The GridView should use the EntityDataSource and both should have Update and Deletes enabled.
Your page code should look like this.
<%@ Page Language=“C#” AutoEventWireup=“true” CodeFile=“Default.aspx.cs” Inherits="_Default" %><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>