Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to update database to match the current model because there are pending changes and automatic migration is disabled

I, for the life of me, can't get rid of this error message. I have tried almost everything that I can.

MyDBContext.cs

        public MyDBContext() : base("ConnStr_Dev")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, DbMigrationsConfiguration<MyDBContext>>());
        //Database.SetInitializer<MyDBContext>(null);
        base.OnModelCreating(modelBuilder);
    }

GenericRepository.cs

        public void Insert(T obj)
    {
        table.Add(obj);      //this is where it throws the error, inserting first time.
    }

Tried all of these in different combinations

Enable-Migrations -EnableAutomaticMigrations -Force

Add-migration Initial -IgnoreChanges

Update-Database

I have tried deleting the Migrations table, deleting the entire database, everything.

Any Migrations experts please?

Edit: the DAL contains the GenericRepository and the context class.

like image 641
who-aditya-nawandar Avatar asked Apr 01 '17 11:04

who-aditya-nawandar


1 Answers

I fought with this error too. I added the migration and then made what I thought was a trivial change.

This property: IMigrationMetadata.Target in your migration isn't random. It's computed based on the state of the model at the time the migration was completed. (That may be an oversimplification.)

So if you make additional changes, it looks as there are additional pending changes that require yet another migration.

In my case, because the changes hadn't been committed to source control, the fix was to delete and re-add the migration. It took a while to figure out the cause because the error message is unclear unless you already know what's causing it. But the lesson I learned (being relatively new to EF) is that changes to the models require either a new migration or re-doing the migration that I'm working on.

like image 155
Scott Hannen Avatar answered Nov 14 '22 21:11

Scott Hannen