Entity Framework 4 (EF4) with Visual Studio 2010 and Pluralisation

With the recent release of Microsoft’s EF4 with Visual Studio there is some debate around the use of pluralisation of table names.

Standard database theory is taught to use singular entity names, i.e. Genre and the table names should match this singular naming convention, i.e. Genre not Genres.

With EF4, whether using the Code First (CF) features or not, the table names are expected to be pluralised. This is an issue if you prefer to stick with the singularised table names or don’t have a choice on pre-existing databases. It is difficult to change an existing database, especially if that database is being used by other programs or reports within an organisation. This also causes some conflicts when an entity such as Person is used – the EF pluralises this as Persons but perhaps People would be a better option.

This post shows how to get around the EF pluralisation for whatever reason you need.

I prefer to use the manual setup of the entity classes as it allows me to set the attributes needed for frameworks such as MVC3 that facilitate ORM mapping and validation.

I’ll use the the MVC3 Music Store tutorial in my example. The database entity setup can be found here: http://www.asp.net/mvc/tutorials/mvc-music-store-part-4. Check the entity relationship diagram in the tutorials and you will see the database tables are all named singularly. This suited EF3 but was broken in EF4 and there is an updated database available, however, the linked scripted database still created the singular table names at time of writing.

The EF4 Code First (CF) model has a nice feature allowing the database schema to be generated from code. If the code schema changes, you can request in code to drop the schema and recreate it. This has some limitations in all but the simplest of schemas and is not an option in production code. Auto generating the schema is outside the scope of this article but is covered in the MVC3 Music Store tutorial.

In a production or complex environment, you will need to modify the database schema manually and reflect the changes in your code. This may mean adding a column or adding a new table to your database.

To specify the Genre table from the tutorial, your entity class would be:

An example of the database context class is below:

The EF will expect the database table name to be Genres (because the entity is called Genre, not because the entity set is called Genres). If you wish to keep the table names as Genre (or use some other name if legacy issues existed), you can override the table name individually by overriding the settings in the OnModelCreating function, as below:

NB: The quoted name specifies the database table name.

You can do this for all tables or just the tables required – you do not have to list all tables, only the table names you wish to override.

Alternatively, and more simply, you can annotate the entity class with the table attribute to specify the table name, which overrides the EF’s defaults, including the pluralisation. An example is below:

This is all there is to it. Searching the web, all I found was negative comments that this couldn’t be done or unnecessarily complex solutions, including questions posted in the tutorial mentioned.


Posted in Code, Development | Tagged , , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">