我的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; }
答案 0 :(得分:2)
外键问题已在rc2:https://github.com/aspnet/EntityFramework/issues/3751
中修复如果迁移未获取模型更改,我建议您查看Migrations [context_name] ContextModelSnapshot.cs文件,以查看EF认为您的模型是什么。您可以尝试手动修复快照文件或删除ContextModelSnapshot.cs文件和所有迁移,然后重新开始初始迁移。