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

ef-code-first ef-migrations entity-framework

Question

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 = "myAdmin@gmail.com" };
            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?

1
12
2/8/2015 1:04:31 PM

Accepted Answer

This worked for me.

Go to Package Manager Console and Run - Update-Database -force

7
8/18/2016 11:17:06 PM

Popular Answer

I am not sure if you found the answer to your problem, but this other answer I found here actually did it for me: Entity Framework model change error

I actually ended up deleting the __MigrationHistory table in SQL Server which I didn't know it was being created automatically.

The article also talks about the option to not generate it I think by using this instruction: Database.SetInitializer<MyDbContext>(null); but I have not used it, so I am not sure if it works like that



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow