[英]Entity Framework Code first Foreign Key may cause cycles or multiple cascade paths
I am getting the following error: 我收到以下错误:
Additional information: Introducing FOREIGN KEY constraint 'FK_dbo.Clubs_dbo.Addresses_Address_Id' on table 'Clubs' may cause cycles or multiple cascade paths.
附加信息:在表'Clubs'上引入FOREIGN KEY约束'FK_dbo.Clubs_dbo.Addresses_Address_Id'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
Club model 俱乐部模型
public class Club
{
[Key]
public int ClubId { get; set; }
//[Required(ErrorMessage = "Club name is required")]
[DisplayName("Club name")]
public string Name { get; set; }
//[Required(ErrorMessage = "Address is required")]
public virtual Address Address { get; set; }
}
Address Model 地址模型
public class Address
{
//[Required]
[Key]
public int AddressId { get; set; }
//[Required(ErrorMessage = "Address is required")]
[DisplayName("Address")]
public string FirstLine { get; set; }
//[Required(ErrorMessage = "Town is required")]
[DisplayName("Town")]
public string Town { get; set; }
//[Required(ErrorMessage = "County is required")]
[DisplayName("County")]
public string County { get; set; }
[StringLength(8)]
[DisplayName("Postcode")]
public string Postcode { get; set; }
}
It is a one to one relationship. 这是一对一的关系。
I have tried several solutions to resolve this problem, removing the required data annotation. 我尝试了几种解决方案来解决此问题,删除了所需的数据注释。
I also tried this code: 我也尝试了这段代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
I tried this solution also: 我也尝试过此解决方案:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Address>().HasKey(a => a.AddressId);
modelBuilder.Entity<Club>().HasRequired(a => a.Address);
}
Where am I going wrong and how do I correct it? 我在哪里出错,该如何解决?
I think the relationship you have between Club
and Address
is not one-to-one, it is one-to-many. 我认为您在
Club
和Address
之间的关系不是一对一的,而是一对多的。 The correct way to configure this relationship is this way: 配置这种关系的正确方法是这样的:
modelBuilder.Entity<Club>().HasRequired(a => a.Address).WithMany();
Now, getting back to your problem, that exception is caused when you have multiple paths of cascade deletes.If the foreign key on the dependent entity is not nullable (like your case), then Code First sets cascade delete on the relationship. 现在回到问题所在,当您有多个级联删除路径时会导致该异常。如果从属实体上的外键不可为空(如您的情况),则Code First会在关系上设置级联删除。 So I ussume that you have another relationship where the
Club
entity is involved, and when you delete a record from the Clubs
table, it is possible this delete will end trying to delete for both side the same record in the Addresses
Table. 因此,我假设您与
Club
实体有关,并且在您从Clubs
表中删除一条记录时,还有另一种关系,这种删除将有可能结束尝试删除Addresses
表中同一条记录的双方的关系。
I suggest you take a look to this post and check if you have a situation like the example that is showed in the @KristofClaes' answer. 我建议您看一下这篇文章,并检查您是否遇到类似@KristofClaes答案中显示的示例的情况。
You can avoid such ambiguous delete paths by either disabling cascading delete using Fluent API or by defining some of the relationships as optional (with a nullable foreign key). 通过使用Fluent API禁用级联删除或通过将某些关系定义为可选关系(使用可为空的外键),可以避免此类歧义的删除路径。 For example using Fluent Api you could configure your relationship as I show below:
例如,使用Fluent Api,您可以按如下所示配置您的关系:
modelBuilder.Entity<Club>().HasRequired(a => a.Address).WithMany().WillCascadeOnDelete(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.