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:
I am confident the POCO table models match what is in the database. Has anyone got any suggestions or thinks I can try?
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.
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
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.
Update-Database –TargetMigration <second_last_migration>
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.
Update-Database
Source https://learn.microsoft.com/en-gb/ef/ef6/modeling/code-first/migrations/teams#resolving-the-merge-conflict
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With