实体框架7不断创建相同的迁移,并且不会进行任何模型更改

时间:2016-04-28 20:54:13

标签: entity-framework asp.net-core entity-framework-core

我的EF7命令:dnx ef migrations add NAME_HERE每次运行时都会不断创建完全相同的迁移。即使我使用dnx ef database update更新数据库,并观察推送到数据库的更改,我也会添加一个新的迁移,它会再次创建相同的内容。

另请注意,迁移不会提升我的模型更改。我从模型中删除的属性未显示在迁移中。

它反复生成的代码是:

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(name: "FK_FooEvent_Organizer_OrganizerForeignKey", table: "FooEvent");
        migrationBuilder.AddForeignKey(
            name: "FK_FooEvent_Organizer_OrganizerForeignKey",
            table: "FooEvent",
            column: "OrganizerForeignKey",
            principalTable: "Organizer",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(name: "FK_FooEvent_Organizer_OrganizerForeignKey", table: "FooEvent");
        migrationBuilder.AddForeignKey(
            name: "FK_FooEvent_Organizer_OrganizerForeignKey",
            table: "FooEvent",
            column: "OrganizerForeignKey",
            principalTable: "Organizer",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
    }
}

我的DbContext看起来像这样:

public DbSet<FaroEvent> FaroEvents { get; set; }
public DbSet<Organizer> Organizers { get; set; }

我的模型与迁移的相关属性是:

public int OrganizerForeignKey { get; set; }

[Required, ForeignKey("OrganizerForeignKey")]
public Organizer Organizer { get; set; }

1 个答案:

答案 0 :(得分:2)

外键问题已在rc2:https://github.com/aspnet/EntityFramework/issues/3751

中修复

如果迁移未获取模型更改,我建议您查看Migrations [context_name] ContextModelSnapshot.cs文件,以查看EF认为您的模型是什么。您可以尝试手动修复快照文件或删除ContextModelSnapshot.cs文件和所有迁移,然后重新开始初始迁移。