Sunday, April 17, 2011

Plural table name error with entity framework 4.1 (code first)

Invalid object name 'dbo.Albums'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.Albums'.

Source Error:

Line 33: // the albums with the highest count
Line 34:
Line 35: return storeDB.Albums
Line 36: .OrderByDescending(a => a.OrderDetails.Count())
Line 37: .Take(count)


If you get this issue then you maybe able to fix it by doing this

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}


and don't forget to reference this

using System.Data.Entity.ModelConfiguration.Conventions;

oh and this needs to be in your a class that inherits DbContext


Hope this helps someone.

No comments:

Post a Comment