Cascading entity deletes
Cascading deletes, if not configured properly, can cause cycles.
Problem
Cascade delete means that if an entity is deleted, all of its dependent entities will be removed too. This makes sense in those cases where a child cannot exist without its parent; imagine a blog and its posts, for example. This is how Entity Framework deletes these dependent entities:
- If we are using a database engine that supports cascades in constraints (such as SQL Server) and Entity Framework was used to create the database, it will use them
- If the dependent entities are not loaded, they will be deleted with a single
DELETE
statement - If we cannot delete them, we can set the relation property to
NULL
; unfortunately, this has to be done one by one for all the related entities
Problems start to arise if we let Entity Framework set up the cascade constraints in the database and there are cycles, meaning the deletion of a record cascades to other records, possibly in different tables, which in...