目前,我遇到了一个问题,我可以创建一个带有数据更新的迁移,但 DbContextModelSnapshot
同时也拥有这些数据。当你查看迁移中的更新时,它们已经被应用到了 DbContextModelSnapshot
和数据库中。
大约 3 个月前,我的同事创建了一个迁移,一切正常。自那以后我们又创建了几个迁移,并且没有遇到这个问题。然而,在过去的几天里,我们一直在遇到这个问题。
我们尝试创建一个具有此数据更新的迁移并运行它,但是当我们再创建一个迁移时,这个数据更新又出现了。我们中没有人之前遇到过这个问题。
我们如何创建迁移而不会让这些数据继续出现呢?我不确定哪些代码是相关的。
编辑:
迁移被创建,其中包含数据 A(数据库和 DbContextModelSnapshot
中的现有数据)和数据 B(新数据),并且完全成功执行。我的意思是,数据 B 被添加到数据库和 DbContextModelSnapshot
中,没有任何问题。然而,当我创建另一个迁移时,数据 A 再次出现了。
编辑 2:
根据要求,我查看了我们所有的迁移并与 __EFMigrationsHistory
进行了比较,它们都在那里。每个迁移都已经成功运行。这已经得到至少三人的确认。
编辑 3:
好的,问题是如何指定迁移的数据。在模型类中,我们有我们的 Configure()
。这是它及其后续函数:
internal static void Configure(EntityTypeBuilder<PlayerType> entityTypeBuilder)
{
SetDefaultValues(entityTypeBuilder);
LoadSeedData(entityTypeBuilder);
}
private static void LoadSeedData(EntityTypeBuilder<PlayerType> entityTypeBuilder)
{
void AddPlayerType(int playerTypeId, string name, DateTime createdDate, DateTime? modifiedDate = null, bool isSystemDefault = true)
{
entityTypeBuilder
.HasData(
DbContextHelper.AssignBaseStats(
new List<PlayerType>
{
new PlayerType()
{
PlayerTypeId = playerTypeId,
Name = name,
IsSystemDefault = isSystemDefault
},
},
createdDate,
!modifiedDate.HasValue ? createdDate : modifiedDate.Value,
ApplicationConstants.SeedDataGenerationIdentifier,
false));
}
AddPlayerType(ApplicationConstants.TitleId, "Title", new DateTime(2020, 2, 11, 16, 27, 0, 0, DateTimeKind.Utc),
new DateTime(2020, 12, 19, 20, 11, 0, 0, DateTimeKind.Utc));
AddPlayerType(ApplicationConstants.BaseId, "Base", new DateTime(2020, 2, 11, 16, 27, 0, 0, DateTimeKind.Utc),
new DateTime(2020, 12, 19, 20, 11, 0, 0, DateTimeKind.Utc));
AddPlayerType(ApplicationConstants.QuizId, "Quiz", new DateTime(2020, 2, 11, 16, 27, 0, 0, DateTimeKind.Utc),
new DateTime(2020, 12, 19, 20, 11, 0, 0, DateTimeKind.Utc));
AddPlayerType(ApplicationConstants.ThreeDId, "ThreeDModel", new DateTime(2020, 2, 11, 16, 27, 0, 0, DateTimeKind.Utc),
new DateTime(2020, 12, 19, 20, 11, 0, 0, DateTimeKind.Utc), false);
AddPlayerType(ApplicationConstants.TestId, "Test", new DateTime(2020, 2, 11, 16, 27, 0, 0, DateTimeKind.Utc),
new DateTime(2020, 12, 19, 20, 11, 0, 0, DateTimeKind.Utc), false);
AddPlayerType(ApplicationConstants.ScoreId, "Score", new DateTime(2020, 3, 25, 21, 59, 0, 0, DateTimeKind.Utc),
new DateTime(2020, 12, 19, 20, 11, 0, 0, DateTimeKind.Utc));
AddPlayerType(ApplicationConstants.ExitId, "Exit", new DateTime(2020, 3, 25, 21, 59, 0, 0, DateTimeKind.Utc),
new DateTime(2020, 12, 19, 20, 11, 0, 0, DateTimeKind.Utc));
AddPlayerType(ApplicationConstants.WalkId, "Walkaround", new DateTime(2020, 10, 22, 14, 44, 0, 0, DateTimeKind.Utc),
new DateTime(2020, 12, 19, 20, 11, 0, 0, DateTimeKind.Utc), false);
}
private static void SetDefaultValues(EntityTypeBuilder<PlayerType> entityTypeBuilder)
{
entityTypeBuilder
.Property(thisTable => thisTable.IsSystemDefault)
.HasDefaultValue(false);
}
现在,这个问题已经在去年十二月中旬修复了,之后的所有迁移都没有问题。但是最近的迁移开始出现以下问题:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.UpdateData(
schema: "LessonDesigner",
table: "PlayerType",
keyColumn: "PlayerTypeId",
keyValue: 1,
column: "IsSystemDefault",
value: true);
migrationBuilder.UpdateData(
schema: "LessonDesigner",
table: "PlayerType",
keyColumn: "PlayerTypeId",
keyValue: 2,
column: "IsSystemDefault",
value: true);
migrationBuilder.UpdateData(
schema: "LessonDesigner",
table: "PlayerType",
keyColumn: "PlayerTypeId",
keyValue: 3,
column: "IsSystemDefault",
value: true);
migrationBuilder.UpdateData(
schema: "LessonDesigner",
table: "PlayerType",
keyColumn: "PlayerTypeId",
keyValue: 6,
column: "IsSystemDefault",
value: true);
migrationBuilder.UpdateData(
schema: "LessonDesigner",
table: "PlayerType",
keyColumn: "PlayerTypeId",
keyValue: 7,
column: "IsSystemDefault",
value: true);
}
编辑4:
以下是屏幕截图,显示了连续创建的迁移。您可以看到,Up()
是相同的: