簡體   English   中英

在表上引入外鍵約束可能會導致循環或多個級聯路徑

[英]Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths

我已經發現了許多其他類似問題,但是我不確定為什么要在我的實體上這樣做。 用下面的簡單英語表示。 三個實體,帳戶,站點和合同。 一個帳戶可以有多個站點。 一個帳戶可以有多個合同。 一個站點可以有多個合同。 因此,在某些情況下,您可以將合同附加到站點,然后將其附加到帳戶,或者將合同直接附加到帳戶。

我假設我正在這樣做,因為有兩種可能的級聯用於刪除合同,它要么在刪除帳戶時級聯,然后直接附加到帳戶,要么在刪除站點時級聯。

我認為合同中既不需要AccountId也不需要SiteId,因此這不會觸發錯誤。 我想要做的是將合同附加到站點(然后是帳戶),或者將合同直接附加到帳戶-但是我仍然想保持數據庫參照完整性。

Introducing FOREIGN KEY constraint 'FK_dbo.Contract_dbo.Site_SiteId' on table 'Contract' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

我的課程/實體:

帳戶

public partial class Account
{
    public int Id { get; set; }

    [Display(Name = "Account Name")]
    [Required]
    [MinLength(3, ErrorMessage = "The Account Name should be at least 3 characters")]
    [MaxLength(40, ErrorMessage = "The Account Name should be no more than 40 characters")]
    [Remote("IsAccountNameAvailable", "Helper", ErrorMessage = "This Account name already exists, please try another name")]
    public string AccountName { get; set; }

    [ScaffoldColumn(false)]
    [Display(Name = "Date Account Added")]
    public DateTime DateAdded { get; set; }

    public virtual ICollection<Site> Sites { get; set; }
    public virtual ICollection<Contract> Contracts { get; set; }
}

現場

public partial class Site
{
    public int Id { get; set; }

    [Required(ErrorMessage = "A site must be attached to an account - please specify an account")]
    [Display(Name = "Parent Account")]
    public int AccountId { get; set; }

    [Display(Name = "Site Name")]
    [Required]
    [MinLength(3, ErrorMessage = "The Site Name should be at least 3 characters")]
    [MaxLength(40, ErrorMessage = "The Site Name should be no more than 40 characters")]
    public string SiteName { get; set; }

    [ScaffoldColumn(false)]
    public DateTime DateAdded { get; set; }

    [Required]
    [Display(Name = "Primary Site")]

    //[Remote("IsThereAlreadyAPrimarySite", "Helper", AdditionalFields = "AccountId", ErrorMessage = "There is already a primary site set for this account", HttpMethod = "POST")]
    public bool PrimarySite { get; set; }

    [Display(Name = "Site Notes")]
    [Column(TypeName = "text")]
    public string SiteNotes { get; set; }

    public virtual Account Account { get; set; }

    public virtual ICollection<Contract> Contracts { get; set; }

}

合同

public partial class Contract
{
    public int Id { get; set; }

    public int AccountId { get; set; }
    public int SiteId { get; set; }

    [Display(Name = "Contract Type")]
    [Required]
    public int ContractTypeId { get; set; }

    public virtual Account Account { get; set; }

    public virtual Site Site { get; set; }

    public virtual ContractType ContractType { get; set; }

}

在您的DbContext文件覆蓋方法OnModelCreating中,必須至少配置一個關系,以便在刪除時不進行級聯。 刪除時必須明確處理該關系

protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
    base.OnModelCreating( modelBuilder );

    modelBuilder.Entity<Contract>()
        .HasRequired( c => c.Site)
        .WithMany( s => s.Contracts )
        .WillCascadeOnDelete( false );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM