Entity Framework Invalid Object Name dbo.Albums

Continuing in working with the MVC Music Store sample application, the next thing I ran into after installing the SQL database by hand, was an error on the home page saying:

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:

// the albums with the highest count
return storeDB.Album
.OrderByDescending(a => a.OrderDetails.Count())
.Take(count)</font>

**Source File:C:DevScratchMvcMusicStore-v2.0MvcMusicStore-CompletedMvcMusicStoreControllersHomeController.csLine:**28

Looking at my database, it looks like this:

Obviously, it should be looking for a table called dbo.Album. This issue is already listed in the CodePlex discussion for this project, with the solution described in a forum post on using EF Code First to change the default plural table name to that of an entity name which is singular. It turns out the solution is pretty simple, although unfortunately you can’t just right-click and edit the properties of some dbContext file in your solution – you have to write a bit of code.

Specifically in this example, you need to go to your MusicStoreEntities file (the class with the same name inherits from DbContext) and add an override of the OnModelCreating() method. The full class with using statements is shown here:

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

namespace MvcMusicStore.Models;
{
 public class MusicStoreEntities: DbContext
 {
 DbSet<Album> Albums { get; set;}
 DbSet<Genre> Genres { get; set;}
 DbSet<Artist> Artists { get; set;}
 DbSet<Cart> Carts { get; set;}
 DbSet<Order> Orders { get; set;}
 DbSet<OrderDetail> OrderDetails { get; set;}

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

And with that, the application runs:

If you liked this post, consider subscribing to email updates about my new posts, or sharing it on Hacker News.

Keyboard shortcuts

?
Show this help
Esc
Close this help or an open menu
n
Next (newer) post: Database cannot be opened – version 655
p
Previous (older) post: Creating a WHERE NOT EXISTS or WHERE NOT IN Query Using LLBLGen
Tab
Move between links and buttons (Shift+Tab to go back)