目前,我遇到了一个问题,在这个问题上,我可以创建一个具有数据更新的迁移,但是DbContextModelSnapshot拥有这个数据。当您查看迁移中的更新时,它们已经应用于DbContextModelSnapshot和数据库。
大约3个月前,一位同事创造了一个迁移,一切都很好。从那以后,我们又创建了几个迁移,而且还没有经历过这个问题。然而,在过去的几天里,我们一直在经历这种情况。
我们尝试用这个数据更新创建一个迁移,并运行它。但是当我们创建另一个迁移时,数据更新再次出现。我们以前都没有经历过这个问题。
我们如何在不不断显示这些数据的情况下创建迁移?我不知道有什么代码可以用来显示这一点。
编辑:
迁移是创建具有数据A(在数据库和DbContextModelSnapshot中预先存在的数据)和数据B(新数据)的迁移,并且完全成功。我的意思是将数据B添加到数据库和DbContextModelSnapshot中,而不存在任何问题。但是,当我创建另一个迁移时,数据A再次出现。
编辑2:
按照要求,我查看了所有的迁移,并将其与__EFMigrationsHistory进行了比较,它们都在那里。每个迁移都已成功运行。至少有3人证实了这一点。
编辑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);
}现在,这是在12月中旬更新的,之后的所有迁移都很好。但如前所述,最近的移民已开始出现以下情况:
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()是相同的:


发布于 2021-02-04 01:36:46
您似乎使用了一些EFC 3.1版本(包含3.1.9个版本),它在一个属性上显示了使用ValueGeneratedOnAdd 的bug #21661 -,它会导致在以后的每个迁移中调用,在3.1.10+中修正#22760 -migration/3.1 Fix问题,其中在每个迁移E 215中都重新加载了在add上生成值的属性。
请注意,按照约定,每个具有HasDefaultValue{Sql}的属性(如您的IsSystemDefault )都被视为ValueGeneratedOnAdd。
现在,要解决这个问题,可以升级到EFC 3.10或更高版本,或者使用下面的ValueGeneratedNever解决方案
entityTypeBuilder
.Property(thisTable => thisTable.IsSystemDefault)
.HasDefaultValue(false)
.ValueGeneratedNever(); // <-- add thishttps://stackoverflow.com/questions/65890151
复制相似问题