Dan Rigsby – Coding Up Style

Developer.Speaker.Blogger

Entity Framework 4.0: Pluralization

Posted by Dan Rigsby on May 19th, 2009

One of the new features in Entity Framework 4.0 (EF v2) is a service call “Pluralization”.  This service is used to convert names of objects form singular and/or plural forms.  For instance, if you have a table in the database that has a plural name such as “Customers”, then Entity Framework will automatically generate both object Name and Entity Set Name to “Customers” as well.  The result is that in your code, the name will look like this:
 
Customers c = new Customers();

Ideally, it would have been nice for EF to see that the table was a plural name and set the object Name to a singular form.

Now, you may be thinking that this is pretty trivial since you can manually change the generated name, but when when you have dozens or hundreds of tables this can be tedious at best.

To use the PluralizationService when generating an Entity Framework model, you just select the option"Pluralize or singularize generated object names”:

Pluralization

Or if you are using the EDMGen.exe tool, you can opt in using the /pluralize option.

Example

In the Northwind database, all the tables have plural names. (Note: It is typically recommended to name your tables in the singular form).

 Pluralization3

When this database is generated using the PluralizationService, the names are set correctly.

Pluralization1 Pluralization2 

Manually using the PluralizationService

The PluralizationService is in the System.Data.Entity.Design.dll under the System.Data.Entity.Design.PluralizationServices namespace.

Pluralization4

It is an abstract class and hence, can’t be directly instantiated.  However, there is a static CreateService method on the class to access instances of the service for various cultures.  At this time, the only culture implemented is “en-us”.

It would have been very nice if this were implemented at a more core level of .Net since this could potentially be useful for a lot more than just for Entity Framework design.  Here are some examples using the service:

// Create an instance of the service for

// a particular culture (only 'en-us' atm)

PluralizationService ps =

    PluralizationService.CreateService(

         CultureInfo.GetCultureInfo("en-us"));

 

// Convert strings to plural or singular

string pluralName = ps.Pluralize("Goose");

string singularName = ps.Singularize("Territories");

// Note: Correctly returns Geese and Territory

 

// Check if strings are plural or singular

bool isPluralName = ps.IsPlural("Sheep");

bool isSingularName = ps.IsSingular("Sheep");

// Note: Correctly sees both of these as true

Adding your own pluralization mapping

There is an interface using by the PluralizationService called ICustomPluralizationMapping that isn’t publicly exposed.  However, since it is implemented, you can cast the service to this interface and make use of the AddWord method to add your own mapping.Pluralization5

In this example, we add a new word called “Foo” with the plural version being “Foos”:

// Create an instance of the service for

// a particular culture (only 'en-us' atm)

PluralizationService ps =

    PluralizationService.CreateService(

        CultureInfo.GetCultureInfo("en-us"));

 

// Cast to the mapping interface

ICustomPluralizationMapping mapping =

    ps as ICustomPluralizationMapping;

 

// Add a new word mapping

mapping.AddWord("Foo", "Foos");

You could then use the service to generate the metadata or use it in some other context:
EntityModelSchemaGenerator generator =

    new EntityModelSchemaGenerator(

        entityContainer,

        ps);

 

var edm = generator.GenerateMetadata(); 

DotNetKicks Image

3 Responses to “Entity Framework 4.0: Pluralization”

  1. Alex James Says:

    …But it seems the community is starting to do our education for us, and beating us (or should I say me) to the punch!

    Dan Rigsby, who is a WCF MVP, has a fantastic in depth post covering our Pluralization Service features…

    Nice one!

  2. Mason Houtz Says:

    Yuck.

    Automagic pluralization is easily the dummest thing Ruby on Rails ever implemented, and that’s saying a lot because DHH wouldn’t recognize a foreign key if I made him digest it. Every time I see this idea stolen and reimplemented in other technologies, I feel like I’m watching more lemmings jump over the cliff. No software should depend on grammatical pluralization rules to join tables in a database. That’s just dumb from every angle.

    Oh well, maybe EF 4 will fix the rest of their travesties and quit wasting time on toys like this.

    Nice summary of a bad feature though.

  3. Mikael Henriksson Says:

    Great post!!

    I was just trying to get better control over this!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>