Invalid object name 'dbo.CustomerRoles'.

Recently i have been building a website for a customer using NopCommerce v2.80. (This platform is implemented using ASP.NET MVC 4 and Entity FrameWork)

This project required me to build some plugins to extend the core functionality.

Here is a great blog by Skyler Severns showing how to build a plugin with data access.

While adding new DTO's and mapping table data I fell fowl of 

Invalid object name 'dbo.CustomerRoles'.

I was confused as I had created several plugins each with ther own data models, connected to the database and all had been fine. It was only when I extended the existing data scheme that the problem arose.

It took a while for the penny to drop that each plugin had its own data context (which I had hand crafted) and had missed the fact that the nopCommerce datacontect had used 'Non Pluralisation'

So to fix this on each data context simply add the following code to OnModelCreating() method.

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

    // Other initialistion
    modelBuilder.Configurations.Add(new RoleAccessCodeMap());

    base.OnModelCreating(modelBuilder);
}

Add comment