共用方式為


Sneak Preview - Pluralization in Entity Framework 4.0

 


The information in this post is out of date.

Visit msdn.com/data/ef for the latest information on current and past releases of EF.


 

In .NET 3.5 SP1 when you create a model from the database, you often end up with EntityType, EntitySet and NavigationProperty names that are not quite right.

Perhaps the most common example is pluralized EntityType names. No one wants to create an instance of a ‘Customers’ entity, but .NET 3.5 SP1 if the underlying database table is called “Customers” that is exactly what you get. Instead you generally want the name to be singularized, so you can create a ‘Customer’ .

In .NET 4.0 these sorts of issues have been addressed. We’ve added simple pluralization services.

The most obvious place that this shows up is when you are building a model from a database on the Choose Your Database Objects screen:

clip_image002

 

Notice the checkbox which allows you to control whether pluralization / singularization should be attempted.

If you leave this on, the Entity Framework will produce a model with:

  • Singularized EntityType names
  • Pluralized EntitySet names
  • Singularized NavigationProperty names that return 0..1 related Entities
  • Pluralized NavigationProperty names that return 0..n related Entities

The service that does this is pretty simple, it is not a full featured pluralization service, it is a naive English only solution. However the solution has been designed so that it can evolve to take advantage of more advanced services as they become available, probably via the OS itself.

In the meantime though you can drop down to the API level and create your own implementation derived from the abstract PluralizationService class. Once you’ve done this you can tell the EntityModelSchemaGenerator to use your implementation, rather than the default, to produce a model using your particular pluralization rules. This is an API only solution, we don’t yet provide a way to choose a different pluralizer in Visual Studio.

In the meantime see the MSDN documentation for more information.

Cheers,
Alex James
Program Manager, Entity Framework Team.

Comments