Invalid object name 'dbo.Genres' ...

Jan 15, 2011 at 6:41 AM

*** Using MVC3 Version ***

When attempting to view the /Store/ page after following the steps to setup the database using code-first with the entity framework, I am presented with an error: "Invalid object name 'dbo.Genres'."  I am, unfortunately, not terribly well versed in SQL/database mechanics, and am having a difficult time finding the source of this error.  I've checked and double checked the code to ensure that it matches the tutorial code (ignoring the case mismatch in the initialization and then use of the MusicStoreEntities object named storeDB), to no avail.  I've searched the web for Invalid object name dbo.* errors, and the results have not proven helpful so far.  There is a mention of an older database in the comments section, with a table called Genre, instead of Genres, but this did not help to clear up my confusion, as the code does not seem to be looking for a table named Genres, it is merely absorbing the Genre tableset into an object attribute of sorts called Genres.  If this is the case, it would make sense to have a Genre table versus a Genres table, which the database reflects when viewed in the Server Explorer.  Thank you for taking the time to read over this.  Please advise.

Thank You,

Scooter

Coordinator
Jan 15, 2011 at 7:03 AM

EF Code First has a default PluralizingTableNameConvention which assumes the Genre information will be in a table named Genres. If you're getting an error that says Invalid object name dbo.Genres, EF is looking for the correct table and not finding it. Can you open the database to see if that table exists? Are you using Web Dev 2010 Express or Visual Studio 2010?

Jan 15, 2011 at 7:07 AM

I am using VS2010.  I am able to view the table structure using Server Explorer.  The table is called Genre, and is not pluralized.  Is there a way to force EF to skip the default pluralization?

--Scooter

Coordinator
Jan 15, 2011 at 7:21 AM

You can turn off the pluralizing name convention by overriding the OnModelCreating method as explained here: http://social.msdn.microsoft.com/Forums/en-ie/adonetefx/thread/719f3e4d-073b-49fe-9968-945acde27bb7

Another option is to rename the table names to plural.

Jan 15, 2011 at 7:24 AM

Thanks!  The override method worked.

-- Scooter

Jan 30, 2011 at 3:16 PM

Can someone please describe a full procedure or way to overiding that method I am confused and I want to know the way to do it in VS2010

I see the entire System.Data.Entity.ModelConfiguration.Conventions.Edm.Db namespace   in referenc eof my project but is there a way to edit or change them?

Jan 30, 2011 at 3:43 PM
scooterwadsworth wrote:

Thanks!  The override method worked.

-- Scooter

Can someone please describe a full procedure or way to overiding that method I am confused and I want to know the way to do it in VS2010

I see the entire System.Data.Entity.ModelConfiguration.Conventions.Edm.Db namespace   in referenc eof my project but is there a way to edit or change them?

Can you descibe the way in visual studio to do it?

thanks

Apr 5, 2011 at 12:51 PM

This blog post explains how it is done - http://stevesmithblog.com/blog/entity-framework-invalid-object-name-dbo-albums/

Coordinator
Apr 6, 2011 at 10:21 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Apr 13, 2011 at 12:59 PM
Edited Apr 13, 2011 at 12:59 PM

What's wrong when my App cant find...

using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;

Jun 4, 2011 at 7:01 PM

I want to note that this error can also happen if you have pre-defined your database.  For example, if you are developming with SQL developer edition, you usually create the database using the sql manager and then set the connection string.      The proper way is to only define the connection string, but make sure the database doesn't exist at all.  When you start the mvc3 app, EF code first will create the database and a table called dbo.EdmMetaData.

Oct 25, 2011 at 1:30 AM

I think that's out-dated. I found this worked:

 

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

 

Jun 10, 2012 at 9:48 PM

Where did you read the steps to set up the database

I am runtime compile error on Genre not being defines as an entity

Coordinator
Jun 12, 2012 at 6:45 AM

@stevensrf1 This tutorial uses Entity Framework Code First, which automatically creates the database based on the entity classes. You shouldn't need to do anything to set up the database.