首页 > 技术文章 > Net core 项目 EF CodeFist 多重外键约束问题

a2502971 2017-10-26 12:21 原文

 

示例如下

[Table("ApprovalLog")]
public class ApprovalLog
{
[Key]
public int LogId { get; set; }
public int Action { get; set; }
public string Approver { get; set; }
public int ApproverId { get; set; }
public string ApproverOrg { get; set; }
public int ApproverOrgId { get; set; }
public string Content { get; set; }
public DateTime CreationDate { get; set; }
public DateTime ApprovalDate { get; set; }
public int DocId { get; set; }
public string Receiver { get; set; }
public int? ReceiverId { get; set; }
public string ReceiverOrg { get; set; }
public int? ReceiverOrgId { get; set; }

[ForeignKey("ApproverId")]
public virtual User ApproverNavigation { get; set; }
[ForeignKey("ApproverOrgId")]
public virtual Org ApproverOrgNavigation { get; set; }
[ForeignKey("ReceiverId")]
public virtual User ReceiverIdNavigation { get; set; }
[ForeignKey("ReceiverOrgId")]
public virtual Org ReceiverOrgIdNavigation { get; set; }
}

[Table("User")]
public class User
{
public User()
{
ApprovalLogApproverNavigation = new HashSet<ApprovalLog>();
ApprovalLogReceiverNavigation = new HashSet<ApprovalLog>();
}
[Key]
public int UserId { get; set; }
public string Account { get; set; }
public bool Enabled { get; set; }
public string Memo { get; set; }
public string Name { get; set; }
public int OrgId { get; set; }
public string Phone { get; set; }
public string PoliceNo { get; set; }
public string Password { get; set; }

[ForeignKey("OrgId")]
public virtual Org Org { get; set; }
public virtual ICollection<ApprovalLog> ApprovalLogApproverNavigation { get; set; }
public virtual ICollection<ApprovalLog> ApprovalLogReceiverNavigation { get; set; }
}

[Table("Org")]
public partial class Org
{
public Org()
{
ApprovalLogApproverOrgNavigation = new HashSet<ApprovalLog>();
ApprovalLogReceiverOrgNavigation = new HashSet<ApprovalLog>();
User = new HashSet<User>();
}
[Key]
public int OrgId { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public int Type { get; set; }

public virtual ICollection<User> User { get; set; }
public virtual ICollection<ApprovalLog> ApprovalLogApproverOrgNavigation { get; set; }
public virtual ICollection<ApprovalLog> ApprovalLogReceiverOrgNavigation { get; set; }
}

执行Add-Migration  Update-Database 生成数据库会出错显示:将 FOREIGN KEY 约束 'FK_ApprovalLog_Org_ApproverOrgId' 引入表 'ApprovalLog' 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

解决方案:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ApprovalLog>(entity =>
{
entity.HasOne(d => d.ApproverNavigation)
.WithMany(p => p.ApprovalLogApproverNavigation)
.HasForeignKey(d => d.ApproverId)
.OnDelete(DeleteBehavior.Restrict);
entity.HasOne(d => d.ApproverOrgNavigation)
.WithMany(p => p.ApprovalLogApproverOrgNavigation)
.HasForeignKey(d => d.ApproverOrgId)
.OnDelete(DeleteBehavior.Restrict);
entity.HasOne(d => d.ReceiverIdNavigation)
.WithMany(p => p.ApprovalLogReceiverNavigation)
.HasForeignKey(d => d.ReceiverId)
.OnDelete(DeleteBehavior.Restrict);
entity.HasOne(d => d.ReceiverOrgIdNavigation)
.WithMany(p => p.ApprovalLogReceiverOrgNavigation)
.HasForeignKey(d => d.ReceiverOrgId)
.OnDelete(DeleteBehavior.Restrict);

});
}

添加如上在DbContext里即可解决

推荐阅读