var genres = storeDB.Genre.ToList(); error

Apr 28, 2012 at 4:44 PM

Hi, I am following instructions in pdf file, starting page 47 where EF is introduced. When runing the application, I am getting the following error

An error occurred while executing the command definition. See the inner exception for details.

Digging into the error, I discovered that the generated sql query is quering a table "Genres" instead of "Genre". I created The tables using the provided sql file against sql server 2008.

1st question, why EF uses the plural of the table name (Genres and not Genre) whereas we have indicated  "public DbSet<Genre> Genre { get; set; }" in class MusicStoreEntities.

I renamed table to genres, code worked. I would like to understand that renaming table would not impact the other exercises, 2nd can not understand or how EF or why it is adding the "s" tpo the table name.

 

May 21, 2012 at 8:52 PM

I am having the exact same problem. Why is the entity framework adding a "s" to the end of dbo.Genre table?

Coordinator
May 21, 2012 at 9:34 PM

By default, Entity Framework pluralizes classes when determining the table name, so Genre class will be placed in Genres table. There's a blog post which explains this information here: ttp://stevesmithblog.com/blog/entity-framework-invalid-object-name-dbo-albums/

Are you using the latest release of MVC Music Store? This should be fixed in the newer scripts.

May 25, 2012 at 11:06 AM
Edited May 25, 2012 at 11:17 AM

I ran into this problem and after looking over the tutorial to see if I had copied in something incorrectly I came to this site hoping for some information. This seems to be the same error I am getting, but I don't know how to change the table names as is suggested by one person. If I try and open a table properties I cannot edit, but can only see the table name. How would I change the properties, or better yet is there a better way to overcome this error?

Also the link posted by jongalloway seems to be broken.

 Edit: I found a link which described how to change a table name.

http://msdn.microsoft.com/en-US/library/bw5xhetd(v=vs.80)

I hope nobody minds that I linked this since it is a Microsoft site; though it won't give you the site direcly it should lead you to it, or you could just google or bing the phrase "how change table name visual studio" or some similar thing.

Note: When "right clicking on table" to select the properties of it, make sure you click to the right side (no directly on the columns).

May 26, 2012 at 8:02 PM
Edited May 26, 2012 at 8:09 PM

I was able to solve the same problem by including the database name into the connectiontionstring.

e.g.  

<add name="MvcMusicStore" connectionString="Data Source=.\SQLEXPRESS;InitialCatalog=MvcMusicStore;Integrated Security=True" providerName="System.Data.SqlClient"/>

- OR - 

You can also directly map the table name by overriding "OnModelCreating" from your DBContext class. 

e.g.        

protected override void OnModelCreating(DbModelBuilder modelBuilder)        

{            //modelBuilder.Entity<Album>().ToTable("Album");        }