After updating my project from dotnet core 2.2 to 3.1. Now, after running migrations add
command, new migration contains update for all seed data.
public class MyEntity
{
public Guid Id { get; private set; }
public string Code { get; private set; }
public string Title { get; private set; }
public bool IsActive { get; private set; }
private MyEntity() { }
[JsonConstructor]
public MyEntity(Guid id, string code, string title)
{
Id = id;
Title = title;
Code = code;
IsActive = true;
}
}
public class MyEntityEntityTypeConfiguration
: IEntityTypeConfiguration<MyEntity>
{
public void Configure(EntityTypeBuilder<MyEntity> configuration)
{
configuration.HasKey(b => b.Id);
configuration.Property(b => b.Title).HasMaxLength(150).IsRequired();
configuration.Property(b => b.Code).HasMaxLength(20).IsRequired();
configuration.Property(c => c.IsActive).HasDefaultValue(true);
configuration.HasData(
new MyEntity(new Guid("3274D8FD-DFAE-4396-8DD0-89A6F5107DB1"), "1", "titl1")
);
}
}
and this is unwanted part of migration, this code exist in all feature migrations.
migrationBuilder.UpdateData(
schema: "dbo",
table: "MyEntity",
keyColumn: "Id",
keyValue: new Guid("08f2aa3d-ad4c-4a82-987c-ff43527466e0"),
column: "IsActive",
value: true);
I didn't have this update before updating to dotnet core 3.1. What is changed?
This issue is being tracked here.
A work around I'm using in my own project to ignore all seed data updates;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "Workaround")]
public class ModelDiffer : MigrationsModelDiffer
{
public ModelDiffer(
IRelationalTypeMappingSource typeMappingSource,
IMigrationsAnnotationProvider migrationsAnnotations,
IChangeDetector changeDetector,
IUpdateAdapterFactory updateAdapterFactory,
CommandBatchPreparerDependencies commandBatchPreparerDependencies) :
base(typeMappingSource, migrationsAnnotations, changeDetector, updateAdapterFactory, commandBatchPreparerDependencies)
{
}
//FIXME Remove when https://github.com/aspnet/EntityFrameworkCore/issues/18943 is deployed
public override IReadOnlyList<MigrationOperation> GetDifferences(IModel source, IModel target)
{
return base.GetDifferences(source, target)
.Where(o => !(o is UpdateDataOperation))
.ToList();
}
}
services.AddDbContextPool<Context>(o =>
{
o.ReplaceService<IMigrationsModelDiffer, ModelDiffer>();
o.UseSqlServer(connectionString, ...
}