我有 2 张桌子:
public class Work
{
public int WorkId { get; set; }
(...)
}
public class Check
{
public int CheckId { get; set; }
(...)
}
然后我想在它们之间添加与有效负载的多对多关系,因此:
public class WorkCheck
{
[Key, Column(Order = 0)]
[ForeignKey("Work")]
public int WorkId { get; set; }
public virtual Work Work { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Check")]
public int CheckId { get; set; }
public virtual Check Check { get; set; }
(...)
}
public class Work
{
public int WorkId { get; set; }
(...)
public virtual IList<WorkCheck> WorkChecks { get; set; }
}
public class Check
{
public int CheckId { get; set; }
(...)
public virtual IList<WorkCheck> WorkChecks { get; set; }
}
在包管理器控制台中,我运行了命令“Add-Migration WorkCheckTableAdded”,没有显示任何错误。
但是之后,当我执行“Update-Database -verbose”命令时,出现异常: “外键 'FK_dbo.WorkCheck_dbo.Work_WorkId' 引用了引用表 'dbo.Work' 中的无效列 'WorkId'。无法创建约束。查看以前的错误。”