Delete Behaviors
Delete behaviors is defined in the Deletebehavior enumerator type and can is passed to the OnDelete fluent API to control Whether the deletion of a principal/parent entity or the severing of the relationship to dependent/child entities should There is a side effect on the Dependent/child entities.
There be three actions EF can take when a principal/parent entity is deleted or the relationship to the child is severed.
- The child/dependent can be deleted
- The child ' s foreign key values can is set to null
- The child remains unchanged
Note
The delete behavior configured in the EF Core model is only applied when the principal entity is deleted using EF Core and the dependent entities are loaded in memory (that is, for tracked dependents). A corresponding cascade behavior needs to be setup in the database to ensure data that is not being tracked by the context has the necessary action applied. If you use EF Core to create the database, this cascade behavior will be setup for you.
For the second action above, setting a foreign key value to NULL are not valid if foreign key was not nullable. (A non-nullable foreign key is equivalent to A required relationship.) In these cases, EF Core tracks, the foreign key property has been marked as null until SaveChanges are called, at which Time an exception was thrown because the change cannot being persisted to the database. This was similar to getting a constraint violation from the database.
There are four delete behaviors, as listed in the tables below.
Optional relationships
For optional relationships (nullable foreign key, or the foreign key is not set at all) it is possible to save a null foreign key value, which result s in the following effects:
Behavior Name |
Effect on Dependent/child in memory |
Effect on Dependent/child in database |
Cascade |
Entities is deleted |
Entities is deleted |
Clientsetnull (Default) |
Foreign Key Properties is set to null |
None |
SetNull |
Foreign Key Properties is set to null |
Foreign Key Properties is set to null |
Restrict |
None |
None |
Required relationships
For required relationships (non-nullable foreign key) it isn't possible to save a null foreign key value, which results I n the following effects:
Behavior Name |
Effect on Dependent/child in memory |
Effect on Dependent/child in database |
Cascade (Default) |
Entities is deleted |
Entities is deleted |
Clientsetnull |
SaveChanges throws |
None |
SetNull |
SaveChanges throws |
SaveChanges throws |
Restrict |
None |
None |
In the tables above, None can result in a constraint violation. For example, if a principal/child entity was deleted but no action was taken to change the foreign key of a dependent/child, Then the database would likely throw on SaveChanges due to a foreign constraint violation.
At a high level:
-
- If You have entities this cannot exist without a parent, and you want EF-take care for deleting the children Automatica Lly, then use Cascade.
- Entities that cannot exist without a parent usually make use of required relationships, for which Cascade is the default.
-
- If you have entities. May or is not having a parent, and you want EF-to-take care of nulling out the foreign key for yo U, then use Clientsetnull
- Entities that can exist without a parent usually make use of optional relationships, for which Clientsetnull are the Defaul T.
- If you want the database to also try to propagate null values to child foreign keys even when the child entity was not load Ed, then use SetNull. However, note that the database must support this, and configuring the database like this can result in other restrictions , which in practice often makes this option impractical. This is why setnull are not the default.
-
- If you don't want EF Core to ever delete a entity automatically or null out the foreign key automatically and then use RESTR Ict. Note that this requires, your code keep child entities and their foreign key values in sync manually otherwise Constra int exceptions'll be thrown.
Original link
Introduction to Deletebehavior in EF core (go from MSDN)