2
  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 :<

6
  • When merging migrations, things get complicated. The safest option is to delete local migrations, merge in the new code into your branch, then recreate your migration. Ordering of migrations is important. Commented Sep 27, 2022 at 10:32
  • @DavidG just tried that and no luck Commented Sep 27, 2022 at 10:57
  • Are you 100% certain you have the same connection string when generating the migration and running it? Commented Sep 27, 2022 at 12:01
  • @Neil um, no? I have to STASH my config changes before merging, which reverts to default connection string (only difference is the database name). Then I merge, have the migration added to my list, then I Git Stash apply my config change and then run update-database and afterward it will tell me, directly after applying the new migration, that there are pending model changes. Commented Sep 27, 2022 at 12:22
  • @DavidG I tried again at the end of the day and it worked. When I first tried I had already spent 4 hours trying to fix and was suffering from problem saturation and stopped applying the proper scrutiny so saw the same error and assumed it was the same changes but it was just something to do with my new migration. Can you post an Answer for me to accept? Commented Sep 28, 2022 at 9:38

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

You can tell I was getting frustrated yesterday because I named my branch "entity-framework-sucks" lol

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.