Entity Framework, MVC 3, Technical

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)

SQL Server 2012, Technical, Visual Studio

Visual Studio 2010 and SQL Server 2012 server explorer integration

SQL Server 2012 Express is now available to download from this link.

After downloading it and installing it, I tried to integrate it into an existing project in Visual Studio through the Server Explorer, and got the following issue:

This server version is not supported. Only servers up to Microsoft SQL Server 2008 are supported
This server version is not supported. Only servers up to Microsoft SQL Server 2008 are supported

This is apparently a known issue, and happens when you right click on “Data Connections” in Server Explorer, and chose “Create New SQL Server Database”.

The workaround:

The work around is to create your database using Management Studio (Just the DB, not the actual schema).

Once it has been created, you can then “connect” to the database in Visual Studio’s Server Explorer:

Server Explorer Data Connections Add Connection

This will allow you to connect to the database created in Management Studio without any issues.

If you didn’t change it during installation, your instance name will be “.\SQLEXPRESS”.