Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

Message par ForumBot »

Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

Message par ForumBot »

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

- the delete will cascade directly to `Side`

- the delete will cascade directly to `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);

```
Répondre

Revenir à « .NET & C# »