EF Core Remove-Migration removing more than just the most recent migration

372 views Asked by At

My team is seeing an issue with Entity Framework Core 2 (code-first) where our snapshot is out of sync.

We first noticed that when creating a new migration, it included a statement that the programmer didn't add but caused the migration to fail when attempting to apply it to the database because it was already there. We were able to work around it by deleting that statement and then applying the rest of the generated migration.

Unfortunately, that was a temporary, rather than permanent, fix. It seems to keep coming back wanting to create and run that extra statement.

I managed to trace the issue to a migration that dropped a table and recreated it (with the same name) with the only difference being to change the Key column to an identity field. (This identity change is the change that keeps getting recreated.) The Designer file for this migration has no differences from the designer file for the previous migration, though. (The related entity also has it as a Identity column.)

After applying the fix (removing the identity change) again, a teammate just tested out running Remove-Migration and found that the changes in each subsequent migration were rolled back as well. A rough visual image:

  • "Bad" migration
  • Migration1
  • Migration2
  • Migration3
  • EmptyMigrationToAttemptToSync

Removing EmptyMigrationToAttemptToSync did delete its migration files but also undid the changes for Migrations 1, 2, and 3.

I manually added the code for the identity change to the snapshot file and each of the Designer files for all the migrations back to the bad migration. When I added a migration, an empty migration was created as expected. When I removed it, it again undid all the changes from Migrations 1, 2, and 3, while leaving my identity change alone.

I know that a worst-case fix would be to wipe out all the migrations back through the bad one and start over, but I'm on a team of nine developers that's also rolled out to multiple environments for testing, so I'm hoping there's another answer.

Any thoughts? Thank you for your help.

0

There are 0 answers