entity-framework – Neither Add-Migration nor Update-Database fails

Question:

Good evening. The essence of the problem is as follows: migrations in EF Code First are no longer applied.

After making changes, I try to perform Add-Migration <name> , I get the message:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

Attempting to execute Update-Database results in the same error.

Following the advice on the Internet, I tried to delete the folder with migrations and the _MigrationHistory table, to do Build Batch – none of this helped.

I tried to completely delete the database and execute Update-Database , the database is created from scratch, but subsequent changes cannot be applied – the same error in the console.

Please tell me if there is a solution. Maybe you need some additional information – I'll provide. Thanks in advance.

Answer:

I know only one case from practice when you can get the situation you described.

I checked it on the first project I came across, where the working base and working migrations are.

This is how I broke:

  1. created the Title1 field in the first table that came Title1
  2. immediately did Add-Migration Asdf1
  3. here I should have done Update-Database – but I pretended that I forgot to apply the migration through Update-Database , so in this step we do nothing
  4. made one more field Title2 in the same table
  5. doing Add-Migration Asdf2

Voila: I get exactly the situation you described:

PM> Add-Migration Asdf2

Unable to generate an explicit migration because the following explicit migrations are pending: [201705272231565_Asdf1]. Apply the pending explicit migrations before attempting to generate a new explicit migration .

PM> Update-Database

Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [201705272231565_Asdf1]. Applying explicit migration: 201705272231565_Asdf1. Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration.

PM>

(Highlighted a key point in bold, loosely translated: remember to apply the migration before trying to create a new one)

How do I fix it:

  1. commenting on the Title2 field
  2. press Update-Database (Asdf1 migration is applied)
  3. uncomment the Title2 field
  4. doing Add-Migration Asdf2
  5. doing Update-Database

Everything is working!

Check if you have the same situation. I am confused by this part of the question: "a database is created from scratch, but subsequent changes cannot be applied" – it turns out that you forget to apply the migration to the database several times in a row before creating a new one?

Scroll to Top