Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Even though model matches I am getting - Unable to update database to match the current model

  • EF 6.2
  • SQL Server 2012

I am getting the exception:

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

I have done my research here, here, here

I have had a solution working for a long time now with no issues. And in fact I made a change to the database a couple of days ago and all was well using the standard add-migration followed by update-database with no issues.

However today I made a change to the database again and did add-migration followed by update-database. But when I run the application I get the above error.

I make sure the migrations run by including the following in my Application_Start:

ConfigurationPlatform configurationPlatform = new ConfigurationPlatform();
DbMigrator migratorPlatform = new DbMigrator(configurationPlatform);
migratorPlatform.Update();

and the configuration class looks as follows:

public sealed class ConfigurationPlatform : DbMigrationsConfiguration<TreasurePlatformDbContext>
{
    public ConfigurationPlatform()
    {
        AutomaticMigrationsEnabled = false;
        AutomaticMigrationDataLossAllowed = false;
        ContextKey = "TreasurePlatform";
    }

    protected override void Seed(TreasurePlatformDbContext aContext)
    {
        // This method will be called every time after migrating to the latest version.
        // You can add any seed data here...
    }
}

I have also tried:

  • Running add-migration again but it produces NO changes
  • Turning on automatic migrations and it STILL complains

I am confident the POCO table models match what is in the database. Has anyone got any suggestions or thinks I can try?

like image 829
TheEdge Avatar asked Jan 28 '18 08:01

TheEdge


1 Answers

Entity Framework stores a snapshot of the model in each migration. It sounds like your snapshot has become out of sync with your current model. There are two potential ways to fix this.

Method 1

This will create a blank dummy migration which contains a snapshot of your latest model but not actual code. Unfortunately it does mean you will have additional code in your project.

Run Add-Migration <pick_a_name> –IgnoreChanges

Method 2

This will rollback your database and then recreate the migration with an updated snapshot.

You can only do this if you haven't pushed the migration to Git or updated any other databases. Otherwise any other updated databases will also need to be rolled back to the second from last migration on step 1 of this process.

  1. Update-Database –TargetMigration <second_last_migration>

  2. Add-Migration <full_name_including_timestamp_of_last_migration>

    You need to include the timestamp so that migrations knows you want to edit the existing migration rather than scaffolding a new one. This will update the metadata for the last migration to match the current model.

  3. Update-Database

Source https://learn.microsoft.com/en-gb/ef/ef6/modeling/code-first/migrations/teams#resolving-the-merge-conflict

like image 110
Caltor Avatar answered Nov 08 '22 00:11

Caltor