Setting up the builder parameter inside the OnModelCreating method in my app DbContext I found many options in OnDelete/DeleteBehavior: Cascade, ClientCascade, ClientNoAction, ClientSetNull, NoAction, Restrict and SetNull.

Searching on the web I found some content about some of them, but not about them all. What are the differences about them?
Most of the active ones can be referred from here and a more nourished documentation here.
The main differences are related to how the corresponding records in the database and the local model will change. The main thing is to pay attention to whether the navigation property is added to the local data context or not.
Cascade (default for the required relationship)
Deletes related records even if they are not added to the local context
ClientCascade
Deletes related records, but only those that have been added to the local context
SetNull
Set the associated foreign key property/column to null, even if it is not added to the local context.
NotAction, Restrict and ClientSetNull (default for optional relationships)
Sets the corresponding foreign key column/property to null only if the related entity is added to the local context. In the documentation, NotAction, Restrict, and ClientSetNull have the same description. When you try to test these parameters, they will work the same way. I assume that these parameters can create different tables in SQL. However, for MS SQL, they are the same (for DeleteBehavior.Restrict creates "ON DELETE NO ACTION", but this is the default configuration for MS SQL). Nevertheless, from the Entity Framework's point of view, the principle of operation should not change for any SQL dialect.
ClientNoAction.
This parameter does not affect the database either with or without related entities in the local context.
Documentation: https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.deletebehavior?view=efcore-8.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With