gpt4 book ai didi

c# - Entity Framework 在没有任何更新的情况下创建迁移

转载 作者:行者123 更新时间:2023-12-03 15:48:05 26 4
gpt4 key购买 nike

目前,我遇到了一个问题,我可以创建具有数据更新的迁移,但 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()是相同的:
enter image description here
enter image description here

最佳答案

您似乎正在使用某些 EFC 3.1 版本(最高 3.1.9 包括在内),该版本显示错误 #21661 - 使用 ValueGeneratedOnAdd在播种的属性上导致 UpdateData在每次后续迁移中调用 ,在 3.1.10+ 中由 #22760 修复- [release/3.1] 修复了在每次迁移中都会重新植入在添加时生成的值的属性的问题 .
请注意,每个属性都有 HasDefaultValue{Sql}喜欢你的 IsSystemDefault被认为 ValueGeneratedOnAdd按照惯例。
现在,要解决此问题,请升级到 EFC 3.10 或更高版本,或使用以下解决方法 ValueGeneratedNever :

entityTypeBuilder
.Property(thisTable => thisTable.IsSystemDefault)
.HasDefaultValue(false)
.ValueGeneratedNever(); // <-- add this

关于c# - Entity Framework 在没有任何更新的情况下创建迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65890151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com