繁体   English   中英

例外:外键约束可能会导致多个周期

[英]Exception: Foreign Key Constraint may cause multiple cycles

我是ASP .NET新手。 我正在尝试使用以下模型进行简单的CRUD。 Paper类中有三个ChartOfAccountId ,因此有三个父子关系。 这些关系ChartOfAccountIdInventory一种导致以下异常:

Introducing FOREIGN KEY constraint FK_dbo.Papers_dbo.ChartOfAccounts_ChartOfAccountIdInventory' on table 'Papers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint.

ChartOfAccount类:

public partial class ChartOfAccount
{
    public ChartOfAccount()
    {
        this.PapersSale = new HashSet<Paper>();
        this.PapersCostOfSale = new HashSet<Paper>();
        this.PapersInventory = new HashSet<Paper>();
    }

    [StringLength(50)]
    [ScaffoldColumn(true)]
    public string Id { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    public virtual ICollection<Paper> PapersSale { get; set; }
    public virtual ICollection<Paper> PapersCostOfSale { get; set; }
    public virtual ICollection<Paper> PapersInventory { get; set; }
}

纸类:

    public class Paper
    {
        [Key]
        [ScaffoldColumn(false)]
        public int Id { get; set; }

        [DisplayName("Name")]
        [Required(ErrorMessage = "Name is required")]
        [StringLength(50)]
        public string Name { get; set; }

        [DisplayName("Chart Of Account For Sale")]
        [Required(ErrorMessage = "Chart Of Account is required")]
        [StringLength(50)]
        public string ChartOfAccountIdSale { get; set; }

        [DisplayName("Chart Of Account For Inventory")]
        [Required(ErrorMessage = "Chart Of Account is required")]
        [StringLength(50)]
        public string ChartOfAccountIdInventory { get; set; }

        [DisplayName("Chart Of Account For Cost Of Sale")]
        [Required(ErrorMessage = "Chart Of Account is required")]
        [StringLength(50)]
        public string ChartOfAccountIdCostOfSale { get; set; }

        [ForeignKey("ChartOfAccountIdSale")]
        public virtual ChartOfAccount ChartOfAccountSale { get; set; }

        [ForeignKey("ChartOfAccountIdInventory")]
        public virtual ChartOfAccount ChartOfAccountInventory { get; set; }

        [ForeignKey("ChartOfAccountIdCostOfSale")]
        public virtual ChartOfAccount ChartOfAccountCostOfSale { get; set; }
   }    

您应该通过fluentApi设置“ WillCascadeOnDelete = False ”,就像这样:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
         modelBuilder.Entity<Paper>()
            .HasRequired(c => c.ChartOfAccountSale )
            //.WithMany()
            .HasForeignKey(c => c.ChartOfAccountIdSale)
            .WillCascadeOnDelete(false);

         modelBuilder.Entity<Paper>()
            .HasRequired(c => c.ChartOfAccountInventory )
            //.WithMany()
            .HasForeignKey(c => c.ChartOfAccountIdInventory )
            .WillCascadeOnDelete(false);

         modelBuilder.Entity<Paper>()
            .HasRequired(c => c.ChartOfAccountCostOfSale )
            //.WithMany()
            .HasForeignKey(c => c.ChartOfAccountIdCostOfSale )
            .WillCascadeOnDelete(false);

}

我对所有chartOfAccounts都使用了必需的标记,这就是它困扰异常的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM