Skip to content

Remove migration often corrupts the model snapshot in team environments  #23911

Closed as duplicate

Description

@MikaelEliasson

I tried to see if this was reported but could not find an issue. I hope it's not a duplicate.

EF Core version: 5.0.2 (Think it's on all)
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 3.1)
Operating system: Windows
IDE: (VS2019 , latest version)

The problem

Remove migration sometimes (quite often) corrupts the ModelSnapshot by removing too much from the snapshot. This happens when two developers create migrations in parallel and then merge these back to the development branch and someone then creates a new migration and try to revert the new one.

EF will then apply the migration snapshot from the latest of the two parallel migrations BUT it should use the merged version of these because none of the parallel migrations contains the changes from the other one in it's migration snapshot.

The issue is then that the next migrations that's added will contain more changes than it should, and most likely break when it's applied to the database.

More specifically the issue is that you can first get this when adding a migration

image

and then when removing the same migrations right after you can get this which obviously removes things from the snapshot that was not added by this migration.

image

And then when adding another new migration after that you will get this operation that tries to add the Text column to the database again.

image

Detailed repro

I blogged about this in more depth https://www.mikee.se/posts/corrupted_model_snapshot_in_ef_core_20210117 and there is also a small sample project for the issue https://github.com/MikaelEliasson/EFCoreSnapshotCorruptionDemo . The blog post should explain the interesting parts of the demo.

Workarounds

Use source control like git to remove migrations instead of using Remove migration in a team environment.

Activity

  1. changed the title [-]Remove migration [/-] [+]Remove migration often creates a corrupted model snapshot in team environments [/+] on Jan 18, 2021
  2. changed the title [-]Remove migration often creates a corrupted model snapshot in team environments [/-] [+]Remove migration often create a corrupted model snapshot in team environments [/+] on Jan 18, 2021
  3. changed the title [-]Remove migration often create a corrupted model snapshot in team environments [/-] [+]Remove migration often corrupts the model snapshot in team environments [/+] on Jan 18, 2021
  4. ajcvickers commented on Jan 19, 2021

    @ajcvickers
    Contributor
  5. bricelam commented on Jan 19, 2021

    @bricelam
    Contributor

    Duplicate of #8880

    I hope to address situations like this (possibly just with warning) as part of #2174

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions