question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Removing a migration can lead to an incorrect snapshot rollback

See original GitHub issue

When removing a migration with dotnet ef migrations remove, a previous snapshot might be restored to an unexpected state.

The root cause seems to be here, that the behavior of our MySqlTypeMappingSource class can change due to options specified by the user in the UseMySql() call (MySqlTypeMappingSource has an IMySqlOptions parameter in its constructor). For example: A user can specify, that a Guid should be mapped to char(36) and vice versa.

While these options get set correctly for an instance used in the model diffing process when removing a snapshot, they do not get set to a second instance that gets created when generating the (restored) snapshot.

While generating the snapshot, the models type mappings get read again (this time without the options and therefore in a false way) which then get cached by RelationalTypeMappingSource. For example: All options are set to their default values on this second instance, so the char(36) store type will now be mapped to System.String instead of System.Guid.

Finally, when the actual C# code for the snapshot gets generated, the previously cached type mappings (that are wrong) get used to generate the properties. The model still contains the correct properties with the correct ClrType, but the type mappings are incorrect. For example: A b.Property<string>("MyGuid") line is generated instead of b.Property<Guid>("MyGuid").

So by adding and then removing a migration, a previous snapshot can get changed.

Interestingly, this is not an issue when adding migrations, only when removing them. When adding migrations, there is also a second instance of MySqlTypeMappingSource being created, but it is either not used to create actual type mappings, or it got properly initialized with user defined options (I didn’t check which one it is).

This is tracked on our repository by https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1002.

I can provide some example code if needed.

Expected behavior

Either the ITypeMappingSource instance that got properly initialized should be used for the final type mappings that get retrieved to generate the property C# code, or the second instance needs to be properly initialized as well, so that a previous snapshot cannot get altered when removing a migration.

Further technical details

EF Core version: 3.1.0 Database provider: Pomelo.EntityFrameworkCore.MySql

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
lauxjpncommented, Oct 8, 2021

@bricelam I can confirm that this is not an issue anymore in EF Core 5.0+.

0reactions
lauxjpncommented, Sep 13, 2020

@thyago-akira While we already have a minimal project for reproducing this issue for Pomelo and EF Core 3.1.x (see https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1002#issuecomment-570198361) it is always good to have more.

Currently we need to check, whether this has already been fixed for 5.0 or not. Since we will release our latest 3.x minor version tomorrow, I will start upgrading Pomelo.MySql to 5.0 as of Monday. We can check the status of this issue, once the fundamental upgrade is done (probably next weekend).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework rollback and remove bad migration
I have about 5 migrations on the database, but I realised that the last migration was bad and I don't want it. I...
Read more >
EF Core 6 - How to fix inconsistency between the snapshot ...
Physically deleting a migration file is a problem because the migration file has a Back method which rolls back that migration. Put the ......
Read more >
Corrupted model snapshot in EF Core and broken migrations
The most common reason for this is that remove migration is actually not working that well and can often remove too much from...
Read more >
Rollbacks and Snapshots
Snapshots are not deleted upon upgrade of an Update and thus can still be rolled back to. Because Rollback performs similarly to Migration,...
Read more >
Solved: Unable to revert to snapshots after migration
After migrating a vm from 3.5 to 4.0, I'm getting this error when attempting to revert to a snapshot: "Detected an invalid snapshot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found