How to Specify Sql Cascade Actions in C#

Have you encountered this error:

"Introducing FOREIGN KEY constraint 'x_y_Target' on table 'z_UsersInRoles' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints."

It appears when you have violated cascading referential constraints. A good example is the ff relationship:
aspnet_Applications->aspnet_Users->aspnet_UsersInRoles aspnet_Applications->aspnet_Roles->aspnet_UsersInRoles As you can see if you delete a single application it could delete the same aspnet_UsersInRoles record. The solution is to add the following code on OnModelCreating override:
modelBuilder.Entity().HasRequired(r => r.aspnetApplications).WithMany(a => a.aspnet_Roles).WillCascadeOnDelete(false);
We disable cascade delete on aspnet_Roles route, because by default cascade delete is on.

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source

Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.

Explore NERV on GitHub →