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 ...

Get Entity Framework Core Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.