What’s New in Entity Framework 4, Part 2: Modeling Changes If you have been working with the ADO.NET Entity Framework, you have probably been extremely eager to get your hands on the next version that is now part of Visual Studio 2010 and .NET 4.0. Long referred to as “EF Version 2,” this version is now called Entity Framework 4 or EF4, to align with the .NET Framework 4.0 version.
Part 1 of this article appeared in the September/October 2009 issue of CODE Magazine and covered API changes. In that article, you will find details on: - T4 support for custom code generation
- The new ObjectSet class
- Lazy loading support
- POCO support
- Enhanced state management methods
EF4 also contains a lot of highly anticipated improvements to the Entity Data Model (EDM) designer as well as some very interesting additions to the EDM itself. This article will focus on these changes. | " | Model defined functions allow you to add functions that are based on items in the model.
| " |
Pluralization and Singularization by the EDM Wizard In the Visual Studio 2008 EDM Wizard, entity names come directly from the names of the database table names from which the model is derived. The same is true for each entity’s EntitySet name (the wrapper for a set of entities) and the navigation properties. If you don’t manually fix up all of these names, coding with these entities can be very confusing. You could have a query like this: from person in context.Person where person.Address.Count()>0 select person;
The query would make more sense if the various pieces of the expression had the proper singular or plural names. By changing the EntitySet of the Person entity to the plural form, People, and changing the navigation property that points to all of the Address entities for that person to the plural form, Addresses, the query is more logical. from person in context.People where person.Addresses.Count()>0 select person;
Fixing up the names in the model in the current version of Entity Framework (hereafter, EFv1), is a critical and oftentimes consuming step. If you have a model with a lot of entities in it, the process can be quite frustrating. Microsoft corrected this behavior in the new EF4 designer. Figure 1 shows the wizard displaying some challenging table names to turn into entity, EntitySet and navigation property names. Some of the table names are singular and some are plural and many of them use words that need much more than the addition of an “s” to pluralize. Brewery, for example, becomes Breweries. Person becomes People. Conversely, some of the already plural table names need more than the removal of an “s” to turn them into a singular name. Addresses is an example of this.  Figure 1: Letting the EDM Wizard fix up entity names. Figure 2: The model created by the EDM Wizard has worked out the proper singular and plural forms of entities and navigation properties. Figure 3: The EntitySet name for the Person entity was correctly pluralized by the EDM Wizard.Below the object list is an option, new to EF4, called Pluralize or singularize generated object names, which is checked by default. As shown in Figure 2 and Figure 3, when the model is generated, all of these names have been worked out. The names of the entities are all correctly singular; the EntitySet names are plural (e.g., People, Addresses, Breweries). Not only are the entity and entity set names worked out, but the navigation properties as well. Navigations that point to an EntityReference are all singular (e.g., Address.Person) and those which point to an EntityCollection are all plural (e.g., Person.Addresses). | & | | 
By: Julia Lerman Julie Lerman is a Microsoft MVP, .NET mentor and consultant who lives in the hills of Vermont. You can find Julie presenting on data access and other topics at user groups and conferences around the world. Julie blogs at thedatafarm.com/blog and is the author of the highly acclaimed Programming Entity Framework (O’Reilly Media). Follow Julie on twitter at julielermanvt.
jlerman@thedatafarm.com |