在表上引入外键约束可能导致循环或多条级联路径

本文关键字:循环 路径 级联 约束 | 更新日期: 2023-09-27 18:06:22

我已经发现了许多这样的问题,但我不确定为什么它在我的实体上这样做。用简单的英语表示如下。三个实体:账户、场地和合同。一个帐户可以拥有多个站点。一个帐户可以有多个契约。一个站点可以有多个契约。因此,在某些情况下,您可以将合同附在网站上,然后将合同附在帐户上,或者直接将合同附在帐户上。

我假设我得到这个是因为删除合约有两种可能的级联,当一个帐户被删除并直接附加到一个帐户时,它是级联的,或者当一个站点被删除时,它是级联的。

我认为在合同中既不需要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 );
}