我有一个 EF Code First 模型,其中包含一个 Foo 表和一个 Bar 表。这是一个多对多的关系,因此 EF 生成了一个名为 FooBars 的联结表:
CreateTable(
"dbo.FooBar",
c => new
{
Foo_Id = c.Int(nullable: false),
Bar_Id = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Foo_Id, t.Bar_Id })
.ForeignKey("dbo.Foos", t => t.Foo_Id, cascadeDelete: true)
.ForeignKey("dbo.Bars", t => t.Bar_Id, cascadeDelete: true)
.Index(t => t.Foo_Id)
.Index(t => t.Bar_Id);
一切都好。现在,我对模型进行了一些更改并添加了迁移。Foo 实体现在有一些额外的 string 和 int 属性,关系或任何东西都没有变化。但是,出于某种原因,EF 现在坚持应该将联结表称为 BarFoos,并希望删除原来的 FooBars 表:
DropForeignKey("dbo.FooBars", "Foo_Id", "dbo.Foos");
DropForeignKey("dbo.FooBars", "Bar_Id", "dbo.Bars");
DropIndex("dbo.Foobars", new[] { "Foo_Id" });
DropIndex("dbo.FooBars", new[] { "Bar_Id" });
CreateTable(
"dbo.BarFoos",
c => new
{
Bar_Id = c.Int(nullable: false),
Foo_Id = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Bar_Id, t.Foo_Id })
.ForeignKey("dbo.Bars", t => t.Bar_Id, cascadeDelete: true)
.ForeignKey("dbo.Foos", t => t.Foo_Id, cascadeDelete: true)
.Index(t => t.Bar_Id)
.Index(t => t.Foo_Id);
DropTable("dbo.FooBars");
显然,我可以将所有记录从 FooBars 复制到 BarFoos 中,但这很烦人,而且当我对模型进行更改并重新生成这个特定的迁移时,我需要继续做一些事情。为什么 EF 坚持认为 junction table 应该突然反其道而行之?我可以做些什么来避免这种情况吗?