Removing a migration can lead to an incorrect snapshot rollback
See original GitHub issueWhen 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:
- Created 4 years ago
- Comments:6 (4 by maintainers)
@bricelam I can confirm that this is not an issue anymore in EF Core 5.0+.
@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).