Entity Framework 创建迁移而没有任何更新

10

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

输入图像描述

输入图像描述


赏金并不能帮助你获得答案。更清晰的描述和添加 [mcve] 才是关键。特别是短语“DbContext 没有被更新”不够清晰。迁移不会“更新上下文”。你的意思是什么?你必须展示一个能够重现和演示问题的例子。 - Gert Arnold
你是如何指定要在迁移中添加的数据的? - Valuator
PiousVenom,为了严谨起见:请进行两次迁移,然后对它们进行差异比较。它们是否完全相同?如果是,那么这是一个非常奇怪的问题。 - Slava Knyazev
你应该能够轻松地剥离除了模态和dbcontext之外的所有内容,并发布剩下的部分。拥有一个可重现的示例将对解决此问题产生奇效。很可能这是微软的一个bug,如果是这样,那么做这个是必要的。 - Slava Knyazev
你能说明正在使用的Entity Framework版本吗? - Durga Prasad
显示剩余7条评论
1个回答

3
您似乎正在使用一些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

非常感谢!添加.ValueGeneratedNever()解决了问题。虽然如此,我会建议团队进行更新,因为那似乎是更好的想法。 - PiousVenom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接