Is it possible to add WillCascadeOnDelete in one direction from many to many relationships

I have the following model:

public class List { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual ICollection<ListRules> ListRule { get; set; } } public class ListRule { public virtual int Id { get; set; } public virtual List List { get; set; } public virtual ICollection<List> Lists { get; set; } } 

A list can have many ListRules. ListRule must belong to one list. A ListRule may also contain 0, 1, or more lists associated with it.

I tried the following bindings:

 modelBuilder.Entity<ListRule>() .HasRequired(x => x.List) .WithMany(x => x.ListRule) .Map(x => x.MapKey("ListId")); modelBuilder.Entity<ListRule>() .HasMany(x => x.Lists) .WithMany() .Map(x => { x.MapLeftKey("ListRuleId"); x.MapRightKey("ListId"); x.ToTable("ListRuleLists"); }); 

Before moving on, I want to clarify the result I'm looking for:

When someone deletes a list, I want the cascade deletion restrictions to automatically delete the relations associated with the list in the ListRule, as well as many of the many relationships between the ListRule and ListRuleLists.

When I try to update the database, I get the error "FOREIGN KEY restriction" FK_ListRuleLists_Lists_ListId in the "ListRuleLists" table may cause loops or several cascading paths. "I understand why this is currently one of the ways to solve it - add WillCascadeOnDelete (false) to the first model binding above.

But this creates an undesirable side effect. Assume that there is one List with one ListRule in which there are no many-to-many relationships in the ListRuleList table. If the user deletes the list, the ListRule will now be lost due to WillCascadeOnDelete (false).

How can I add the WillCascadeOnDelete (false) add to the RightKey side of the second binding? In other words, I want to remove cascading deletion from the foreign key constraint "FK_ListRuleLists_Lists_ListId".

Is it possible? If not, are there other ways to solve this problem?

+7
source share

All Articles