Reputation: 29
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. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
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?
Upvotes: 1
Views: 716
Reputation: 39326
I think the relationship you have between Club
and Address
is not one-to-one, it is one-to-many. 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. 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.
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.
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). For example using Fluent Api you could configure your relationship as I show below:
modelBuilder.Entity<Club>().HasRequired(a => a.Address).WithMany().WillCascadeOnDelete(false);
Upvotes: 1