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

时间:2015-08-20 09:12:13

标签: c# asp.net-mvc ef-code-first foreign-key-relationship

我是ASP .NET的新手。我正在尝试使用以下模型进行简单的CRUD。 ChartOfAccountId类中有三个Paper,因此有三个父子关系。其中一个关系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; }
   }    

2 个答案:

答案 0 :(得分:0)

你应该设置&#34; WillCascadeOnDelete = False &#34;通过fluentApi,像这样:

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);

}

答案 1 :(得分:0)

我在所有chartOfAccounts上使用了必需的标记,这就是为什么它会引起异常。