The ADO.NET Entity Framework (EF) was released last summer as part of Visual Studio (VS) 2008 SP1. This was the first round of Microsoft's long-term vision for the Entity Framework. VS 2010 brings us the next evolution of the Entity Framework and includes many major enhancements that are a result of valuable feedback from the community.
While there are myriad improvements throughout the Entity Framework, this article will focus on 11 of the most significant new features and changes in this version. Although this is the second iteration of EF, it is being referred to as Entity Framework 4 (EF4), to align with .NET 4.0, the new version of the .NET Framework. Two of the features covered in this article will be included in a separate installation referred to as the Entity Framework Feature Community Technology Preview (CTP). Eventually these will be rolled into Visual Studio and .NET, but not for the VS 2010 release to manufacturing (RTM). This article was written using VS 2010 Beta 1. I've noted known changes for Beta 2 and RTM.
1. Better Entity Naming by the EDM Wizard
One of the biggest problems with the VS 2008 Entity Data Model (EDM) designer is that the wizard does not automatically apply singular and plural names to entities when they're being generated from the database. An entity is a single item, therefore it is logical for its name to be singular. An EntitySet is a wrapper for a set of entities, therefore its name should be plural. However, the wizard merely duplicates the name of the tables from which the model is derived. As a result, developers spend a lot of time manually renaming entities, their EntitySets, and navigation properties in new models. This has been very frustrating, especially when there are a large number of entities in the model.
The EF4 wizard can now apply the appropriate names throughout the model. Figure 1 shows an example of a SalesOrderHeader entity. The wizard automatically applied the correct singular and plural forms to this entity. The navigation properties now relate to the multiplicity of their related entities (e.g., Address points to a single Address while SalesOrderDetails points to a collection of detail entities).
Not only does the wizard determine whether singular or plural is correct, but it also is capable of properly fixing up every English table name I've tried so far. For example, it correctly gives me People as the plural of Person and Breweries as the plural for Brewery. If the table name starts out as a plural name, the wizard ensures that the entity name is singular.
2. Model-First Design
Entity Framework version 1 (EF1) targets developers who want to build a model from an existing database. EF4 now allows developers to use the EDM Designer to create a model, then generate database schema from that model. The Designer has a new context menu option, Generate Database Script from Model. This feature will build a database script that you can then run in your database. The script doesn't create the actual database file but all the tables, keys and constraints based on the model. You'll need to be explicit in the model about defining the attributes of properties. The updated designer provides more control over attributes such as StoreGeneratedPattern (e.g., auto-incremented values), foreign key constraints, and cascade deletes. Be aware that running the script against an existing database will overwrite any existing data.
Model-first design in EF4 depends on a combination of a T4 template and a workflow, which makes the generation completely customizable. Look for more information coming from the Entity Framework team on how to customize the generation of your database scripts.
3. Complex Type Support in the Designer
Complex types are an essential element in the EDM. They let you encapsulate a set of fields into a property. Unfortunately, the EF1 designer does not support complex types, which makes it very difficult to work with them. The new designer allows you to define and reuse complex types in your model. In Figure 2, I've selected Customer properties that define a Customer's name, then chose to refactor them into a complex type.
The results of this refactoring are shown in Figure 3. The entity contains the complex type property which, unfortunately, cannot be expanded. But you can see the different properties of Name in the mappings and how they map back to the table columns.
As with EF1, the complex types are available in code when querying and working with the entity objects.
IEnumerable<Customer> contactsQuery = from c in context.Customers
orderby c.Name.LastName
select c;
The object contains the complex type, as shown in the debug window in Figure 4.
4. New Way of Generating Classes from the Model
EF has traded in the proprietary code-generation API that was part of EF1 for Visual Studio's T4 Code Generator. T4 (Text Template Transformation Toolkit) is a powerful tool that was introduced in VS 2008. With T4, you create code generation instructions by combining T4 syntax with C# or Visual Basic, then link this file to some metadata, such as an .edmx file. T4 will automatically process the template against the metadata and create a new code file based on the output.
EF4 contains a default template that's used to generate classes from the EDM. But you can override this default. The EDM Designer has a context menu option, Add New Artifact Generation Item, which lets you take control of the template so that you can customize it. (Note that the menu option name might change by RTM.) EF copies the template into your project (see the .tt file in Figure 5); then all subsequent code generation from your model will come from this template. By customizing the template, you can take ownership of the definition of the classes that are built based on the model.