Data Enhancements in .NET 4 / Visual Studio 2010 Beta 2

On Wednesday we are releasing .NET 4 / Visual Studio 2010 Beta 2 to the public and  it brings many cool enhancements to the ASP.NET data controls and Entity Framework. I’m going to drill into a few features of these data related features and you can read more about all the changes to ASP.NET 4 at: https://www.asp.net/learn/whitepapers/aspnet40/. This contains a very detailed list of all the changes we have made in ASP.NET.

Here are a couple of my favorite “data” related features:

1) The Entity Framework now supports the ability to the ability to pluralize entity sets and singularize entities. This means when I refer the table the table will be called “Products” but the actual class that I work on in my application is a “Product”. This feels much more natural then Entity Framework 1 where I would write:

Products product = new Products();

while in 4.0 this becomes:

Product product = new Product();

which feels more natural and looks better in my code.

Here is the UI in the Entity Model Wizard for enabling this (note it turned on by default)

image

2) The Entity Framework now supports the ability to include “foreign keys” in the data model. In version 1 of the Entity Framework if I had a Product table that contained a CategoryID which referenced a record in the Category table the generated Entity Framework model would convert the CategoryID into a navigation property which would drill directly in the category table. While this simplified some scenario’s it made web scenarios more difficult. Normally I would represent the this in using a DropDownList control which would contain the primary key and the name of the records from the category table. When saving saving the record I would just want to assign the selected CategoryID from the DropDownList to the Product class. But with navigation properties I would have to revert to some very ugly and non-discoverable code for setting the CategoryID since it not exist. Entity Framework 4 now includes the ability to have foreign key columns added directly to the entity allowing me to either set the foreign key or the navigation property making web scenarios easier to program:

 

imageimage

           EF 1.0                    EF 4.0 

3) QueryExtender support is now available for EntityDataSource. In .NET 4 Beta 1 and in the Dynamic Data Preview releases on CodePlex we introduced a very exciting extender control for the LinqDataSource called the QueryExtender. We created this control in response to feedback we received after releasing Dynamic Data 1.0 that our customers wanted an easy way of filtering or searching the data that their data source controls returned aka “How do I implement a search”. This pattern with data source controls can get fairly complicated and we wanted to provide something simple. The QueryExtender allows the following types of searches:

Search: Searched for a string across any number of columns

Range: Filters data based on if the value of a column is between a given range

Property: Filters based on a column matching a value

Custom: Event is fired in which the developer can customize the LINQ expression.

Here is an example of using the QueryExtender with the EntityDataSource. It takes the value from TextBox1 and uses compares it to the ProductName field and only returns rows which have the text from TextBox1 in the ProductName field:

<asp:EntityDataSource ID="EntityDataSource1" runat="server"
        ConnectionString="name=NORTHWNDEntities"
        DefaultContainerName="NORTHWNDEntities" EnableDelete="True"
        EnableFlattening="False" EnableInsert="True" EnableUpdate="True"
        EntitySetName="Products">
</asp:EntityDataSource>
<asp:QueryExtender ID="QueryExtender1" runat="server" TargetControlID="EntityDataSource1">
<asp:SearchExpression SearchType="Contains" DataFields="ProductName">
<asp:ControlParameter ControlID="TextBox1" />
</asp:SearchExpression>
</asp:QueryExtender>

4) EnableDynamicData extension method. We introduced Dynamic Data in .NET 3.5 SP1 but it put some serious demands on the developer: Required the developer to be using Linq to SQL or Entity Framework, Required the developer to start a new project to get the Dynamic Data support files, Provided a different programming model. With Beta 2 we have added this new feature which can be enabled on our data controls like this:

ListView1.EnableDynamicData(typeofProduct))

This one line of code will automatically bring many of the features that Dynamic Data provides:

- Automatic validation

- Support for Data Annotations on objects to control validation and display properties

- Support for field templates for customizing UI behavior based on data type

This should allow any developer to utilize the power of Dynamic Data without radically changing their application or requiring Linq to SQL or Entity Framework.

There are many other changes and updates to our data support and I will try and blog about it over the next few weeks.

Comments

  • Anonymous
    October 22, 2009
    Where is the POCO support I've been reading about ? Is there a trick to setting this up ? Also, does EF support hi/lo yet ?   Thanks

  • Anonymous
    October 22, 2009
    The comment has been removed