简体   繁体   English

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

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

I've found a number of other questions like this already but I'm not sure why it's doing it on my entities. 我已经发现了许多其他类似问题,但是我不确定为什么要在我的实体上这样做。 In plain English the below representation. 用下面的简单英语表示。 Three entities, Account, Site and Contract. 三个实体,帐户,站点和合同。 An Account can have multiple sites. 一个帐户可以有多个站点。 An Account can have multiple contracts. 一个帐户可以有多个合同。 A site can have multiple contracts. 一个站点可以有多个合同。 So in some instances you can have a contract attached to a site, which is then attached to an account or a contract attached directly to an account. 因此,在某些情况下,您可以将合同附加到站点,然后将其附加到帐户,或者将合同直接附加到帐户。

I'm assuming I'm getting this because there are two possible cascades for the deletion of a contract, it is either cascaded when an account is deleted and it is directly attached to an account OR it is cascaded when a site is deleted. 我假设我正在这样做,因为有两种可能的级联用于删除合同,它要么在删除帐户时级联,然后直接附加到帐户,要么在删除站点时级联。

I thought as neither AccountId or SiteId are required in Contract, that this should not trigger the error. 我认为合同中既不需要AccountId也不需要SiteId,因此这不会触发错误。 What I want to be able to do is attached a contract to a site (and subsequently an Account) or attach the contract directly to an account - but I still want to maintain database referential integrity. 我想要做的是将合同附加到站点(然后是帐户),或者将合同直接附加到帐户-但是我仍然想保持数据库参照完整性。

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.

My classes/entities: 我的课程/实体:

Account 帐户

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

Site 现场

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

}

Contract 合同

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

}

In your DbContext file override method OnModelCreating where at least one the relations must be configured not to cascade on delete. 在您的DbContext文件覆盖方法OnModelCreating中,必须至少配置一个关系,以便在删除时不进行级联。 That relation will have to be explicitly handled on deletion 删除时必须明确处理该关系

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.

相关问题 ef核心2-在表&#39;Y&#39;上引入FOREIGN KEY约束&#39;X&#39;可能会导致循环或多个级联路径 - ef core 2 - Introducing FOREIGN KEY constraint 'X' on table 'Y' may cause cycles or multiple cascade paths 在表“模型”上引入FOREIGN KEY约束“列”可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths 表&#39;UsageSummaries&#39;上的多态与引入FOREIGN KEY约束可能会导致循环或多个级联路径 - Polymorphism vs Introducing FOREIGN KEY constraint '' on table 'UsageSummaries' may cause cycles or multiple cascade paths 在表table上引入FOREIGN KEY约束键可能会导致循环或多个级联路径。 指定ON DELETE…错误 - Introducing FOREIGN KEY constraint key on table table may cause cycles or multiple cascade paths. Specify ON DELETE … Error 当不需要属性时,引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign Key Constraint may cause cycles or multiple cascade paths when property is not required 错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径-为什么? - Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 引入FOREIGN KEY约束可能会导致EF Core中的循环或多个级联路径 - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core 避免“引入FOREIGN KEY约束可能会导致循环或多个级联路径” - Avoiding 'Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM