Using Entity Framework what is the difference between Cascade and ClientCascade?

Viewed 550

I am learning C# and how to use Entity Framework. I believe that I know what is Cascade, Restrict, NoAction and SetNull behaviors.

But there are other behaviors with the prefix Client:

enter image description here

I would like to understand what are the difference between them and when should I use ClientCascade instead of Cascade or ClientNoAction instead of NoAction.

2 Answers

the short answer is-- when dependent/ref. entities are loaded/cached in memory, the cascade delete action fails on the main entity - because a CYCLIC ref. LOCK.

ClientCascade allows the DBContext to delete entities even if there is a CYCLIC ref. LOCK

EF Core Cascades vs. Client Cascades Delete

The Context deletes the entities it tracks, when we delete the parent.

Cascade option creates a migration script with ON DELETE CASCADE, while ClientCascade creates a migration script with ON DELETE NO ACTION.

Cascade is the default behavior, when using the required relationship i.e Foreign key is Not Nullable

Untracked entities if any are deleted if the ON DELETE CASCADE is setup in the database. If not results in a FOREIGN KEY violation.

Not all databases support Cascade or does not support fully. Especially if there are cycles in relationships. Use ClientCascade and load all related entities before deleting the parent. The context will perform the Cascade Delete on the client-side.

also read: https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete

Related