Home > Software engineering >  Entity Framework won't detect that model change is already applied to database
Entity Framework won't detect that model change is already applied to database

Time:09-29

  1. Merged development into my branch
  2. New migration added that adds a column, migration is inserted before my migration
  3. Execute update-database and the migration is applied. Migration is visible in _MigrationHistory table. The column now exists and the model change is applied to db.
  4. EF refuses to believe the model change has been applied and keeps trying to re-generate the same migration when I execute add-migration.

update-database keeps generating this error:

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

How can I tell EF that the model is already applied to db? I can't find, anywhere, how add-migration identifies the changes that need to be put into a migration.

I have no Snapshot file anywhere that can be used. I have tried using fresh, empty databases and applying everything from development and then applying everything from my branch, always the same error.

<TargetFramework>net6.0</TargetFramework>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.7">

EDIT

I tried deleting my migrations before merging with development but, even with my migrations out of the picture (fresh db etc) the same issue happens.

Development branch is fine, development branch knows the migration is applied when update-database is called. As soon as development is merged into my branch it can't identify the changes from the new migration are being applied :<

CodePudding user response:

Migrations in Entity Framework need to be done in a strict order. This is because that's how database work. Imagine you have a set of 3 scripts to make changes to the database, if you run them out of order, you can end up with some very odd results or errors. To fix your code here you should:

  1. Revert your local codebase to be at a point before any migrations have happened, including the migration you made. You will likely also have to rollback your database to the migration before you started applying yours, something like this:

    dotnet ef database update <name-of-old-migration>
    
  2. Pull in the changes from the other branch that has new migrations.

  3. Apply the incoming migrations

    dotnet ef database update 
    
  4. Re-apply your local changes and add a new migration:

    dotnet ef migrations add <name-of-new-migration>
    
  5. Apply the new migration to your database:

    dotnet ef database update 
    

Pro tip: Get this migration added to your central repository ASAP before someone else adds yet another migration!

  • Related