Entity Framework – Plural and Singular Table names

By default, the Entity Framework will assume that all of the names of your tables in your database are either pluralised, or in the case of code first, you would like them to be pluralised when created.

E.g. you have a table called “Product” and not “Products”, or you want your table to be called “Product” and not “Products”

This is the problem that I had. My MVC application consisted of one web page that just dumped out the contents of the “Product” table onto the page. When I browsed to the page, I got an “Invalid object name ‘dbo.Products’.” yellow screen of death runtime error.

The Solutions

1. Rename the table to “Products”. I didn’t want to do this as I’m from the school of singular table names. I was also curious about situations where the tables couldn’t be renamed.

2. Make use of Entity Framework’s fantastic Conventions, that allow you to specify how you have or want your database to be setup.

To tell Entity Framework not to pluralise database table names, simply add the following code into your DbContext class:

public class EfDbContext : DbContext
 {
  public DbSet<Product> Products { get; set; }
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  }
 }

This code will remove the Pluralising convention that is by default attached to all model builders. You will then be able to access database tables with Singular names.

Links

Table Naming Dilemma: Singular vs. Plural Names (StackOverflow)

PluralizingTableNameConvention Class (MSDN)

Advertisements

8 comments

  1. Hi, wondering if you know why my database name comes out as Test1.Models.ClientDBContext from code first? My connection string points Initial Catalog=Test1.

    Are you saying there are restrictions on the naming conventions, and that the ‘1’ is causing this?

  2. I’m new to using Entity Framework, and I was thinking WTF with the pluralization of table names. Why would that be the default when classical computer science teaches singular as standard? What a puzzle?

  3. Thank you very much : )
    I wasted a lot of time trying to fix the problem with table name, thank you again, very good article.

  4. Hey Ed, I don’t suppose you know if there’s a way to do this per-table?
    EF wants to pluralise my “Media” table (and I’m supporting an existing app, so I’ll have to continue to keep things pluralised), but it’s incorrectly pluralising Media to “Medias”.

  5. Jamie: If you’d like to disable the pluralise for only one table (“Media” in your case), you should do this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    //Map entity to table
    modelBuilder.Entity().ToTable(“Media”);
    }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s