[英]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: 我的课程/实体:
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; }
}
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.