我想创建两个表,其中一个表包含两个引用另一个表的列,并且遇到一些问题。 所以,我的模型中有一个Employee类:
public class Employee
{
public Guid Id { get; set; }
[ForeignKey("EmployeeId")]
public virtual ICollection<EmployeeRequest> EmployeeRequests { get; set; }
}
每个员工都可以创建EmployeeRequest,因此我们有一个导航属性。 EmployeeRequest看起来像这样:
public class EmployeeRequest
{
public Guid Id { get; set; }
public Guid ManagerId { get; set; }
public virtual Employee Manager { get; set; }
public Guid EmployeeId { get; set; }
public virtual Employee Employee { get; set; }
}
除了EmployeeId,EmployeeRequest还必须有ManagerId。经理也是员工和必填字段。 迁移代码如下:
public partial class employee_request : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.EmployeeRequest",
c => new
{
Id = c.Guid(nullable: false),
ManagerId = c.Guid(nullable: false),
EmployeeId = c.Guid(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Employee", t => t.EmployeeId, cascadeDelete: true)
.ForeignKey("dbo.Employee", t => t.ManagerId, cascadeDelete: true)
.Index(t => t.ManagerId)
.Index(t => t.EmployeeId);
CreateTable(
"dbo.Employee",
c => new
{
Id = c.Guid(nullable: false),
})
.PrimaryKey(t => t.Id);
}
public override void Down()
{
DropForeignKey("dbo.EmployeeRequest", "ManagerId", "dbo.Employee");
DropForeignKey("dbo.EmployeeRequest", "EmployeeId", "dbo.Employee");
DropIndex("dbo.EmployeeRequest", new[] { "EmployeeId" });
DropIndex("dbo.EmployeeRequest", new[] { "ManagerId" });
DropTable("dbo.Employee");
DropTable("dbo.EmployeeRequest");
}
}
但是在尝试更新数据库时,我收到以下错误:
引入FOREIGN KEY约束 &#39; FK_dbo.EmployeeRequest_dbo.Employee_ManagerId&#39;桌子上 &#39; EmployeeRequest&#39;可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN 关键约束。无法创建约束。查看以前的错误。
有人可以解释一下如何配置上下文以便它可以使用这样的配置吗?