zoukankan      html  css  js  c++  java
  • Net core 项目 EF CodeFist 多重外键约束问题

    示例如下

    [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里即可解决

  • 相关阅读:
    Spring 签名加密+xml数据交互
    Spring 使用 RestTemplate 模拟 电商网站+支付平台
    SSM 框架搭建
    SpringMVC 登录验证实例
    四.Mybatis 动态sql语句
    三.Mybatis 多对一与一对多
    二.Mybatis 增删改查
    一.Mybatis 入门
    SSH框架搭建
    二)Spring AOP编程思想与动态代理
  • 原文地址:https://www.cnblogs.com/a2502971/p/7736162.html
Copyright © 2011-2022 走看看