I\'ve been wrestling with this for a while and can\'t quite figure out what\'s happening. I have a Card entity which contains Sides (usually 2) - and both Cards and Sides h
Because Stage
is required, all one-to-many relationships where Stage
is involved will have cascading delete enabled by default. It means, if you delete a Stage
entity
Side
Card
and because Card
and Side
have a required one-to-many relationship with cascading delete enabled by default again it will then cascade from Card
to Side
So, you have two cascading delete paths from Stage
to Side
- which causes the exception.
You must either make the Stage
optional in at least one of the entities (i.e. remove the [Required]
attribute from the Stage
properties) or disable cascading delete with Fluent API (not possible with data annotations):
modelBuilder.Entity()
.HasRequired(c => c.Stage)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity()
.HasRequired(s => s.Stage)
.WithMany()
.WillCascadeOnDelete(false);