Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The model backing the 'DataContext' context has changed since the database was created

I am trying to use Code First with Migrations. Even though there are no current changes to my model, I'm getting an exception. When I add a migration, the up and down are empty, but I get a runtime error with the message as follows:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The model backing the 'MyDataContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?

My architecture is as follows:

  • DataAccess project that includes the context, fluid configurations and migrations code
  • Model project that contains the poco classes
  • Web API and MVC projects that each contain the connections string in their respective web.config files.

Additionally I have the following code:

DbInitializer

public static MyDataContext Create()
{
   Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDataAccess.MyDataContext, MyDataAccess.Migrations.Configuration>());
   return new MyDataContext(ConfigurationManager.ConnectionStrings["MyDataContext"].ConnectionString, null);
}

I started with AutomaticMigrationsEnabled = false; in the migration Configuration constructor, as it was my understanding that this would allow (and require) me to have more control over when migrations were applied. I have also tried setting this to true but with the same result.

I added a new migration upon receiving this error, and the Up method was empty. I updated the database to this new migration, and a record was created in the _migrationHistory table, but I still receive the error when I attempt to run the application. Also, the seed data was not added to the database.

protected override void Seed(MyDataAccess.MyDataContext context)
{
            IdentityResult ir;

            var appDbContext = new ApplicationDbContext();
            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(appDbContext));
            ir = roleManager.Create(new IdentityRole("Admin"));
            ir = roleManager.Create(new IdentityRole("Active"));
            ir = roleManager.Create(new IdentityRole("InActive"));

            var userNamager = new UserManager<User>(new UserStore<User>(appDbContext));

            //  assign default admin
            var admin = new User { UserName = "administrator", Email = "[email protected]" };
            ir = userNamager.Create(admin, "myp@55word");

            ir = userNamager.AddToRole(admin.Id, "Admin");
 }

where

public class ApplicationDbContext : IdentityDbContext<User>
{
    public ApplicationDbContext()
        : base("MyDataContext", throwIfV1Schema: false)
    {
    }
    ...

The question: If Add-Migration isn't seeing any change in the model, why do I get this error when I run? Why isn't the seed code being hit? How do I fix this, or if that can't be determined, how do I further determine the root cause?

like image 583
Steve Wash Avatar asked Feb 02 '15 11:02

Steve Wash


1 Answers

I bet your data context is not hooking up the connection string. Check if it's not initialized with a localdb (something like (localdb)\v11.0) and not working with that when you might think it's set to something else.

like image 157
George Polevoy Avatar answered Sep 20 '22 15:09

George Polevoy