答案 0 :(得分:0)
如图所示,您只有两张桌子。不可能将它们之间的关系设置为多对多(只有当新对出现时才会向此表添加新列,但这是非常糟糕的做法)。您应该创建第三个表,其中包含主键对。在迁移时,您可以在每个主表和第三个主表之间将cascadeDelete指定为true。见下文:
<强>型号:强>
public class BugReport
{
public BugReport()
{
dublicates = new HashSet<DublicateBugReport>();
}
public int ID { get; set; }
public virtual ICollection<DublicateBugReport> dublicates { get; set; }
}
public class DublicateBugReport
{
public DublicateBugReport()
{
reports = new HashSet<BugReport>();
}
public int ID { get; set; }
public virtual ICollection<BugReport> reports { get; set; }
}
移民片段
public override void Up()
{
CreateTable(
"BugReports",
c => new
{
ID = c.Int(nullable: false, identity: true),
})
.PrimaryKey(t => t.ID) ;
CreateTable(
"DublicateBugReports",
c => new
{
ID = c.Int(nullable: false, identity: true),
})
.PrimaryKey(t => t.ID) ;
CreateTable(
"DublicateBugReportBugReports",
c => new
{
DublicateBugReport_ID = c.Int(nullable: false),
BugReport_ID = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.DublicateBugReport_ID, t.BugReport_ID })
//pay attention! - cascadeDelete: true
.ForeignKey("DublicateBugReports", t => t.DublicateBugReport_ID, cascadeDelete: true)
.ForeignKey("BugReports", t => t.BugReport_ID, cascadeDelete: true)
.Index(t => t.DublicateBugReport_ID)
.Index(t => t.BugReport_ID);
}