Skip to content

Enabling Cascade Delete in EF Core Code First DB Using the Fluent API

Entity Framework Core Default Behaviour on Delete

Cascade delete saves a developer time by not needing to write boilerplate code for related data when the parent data in the relationship has been deleted. However in Entity Framework Core it is not the default behaviour. It takes a more conservative view and sets the on delete behaviour to restrict (StackOverflow Question where EF Core Team Member Confirms) which the documentation defines as:

Restrict: The delete operation is not applied to dependent entities. The dependent entities remain unchanged. – EF Core Documentation

Compared with the definition of cascade:

Cascade: Dependent entities are also deleted. – EF Core Documentation

Enabling Cascade Delete Using The Fluent API

If you have created your database using Code First then the fix is simple. When defining your relationships tell EF that you want the delete action to be a cascade one. This needs the use of an additional using Microsoft.Data.Entity.Metadata which contains DeleteBehaviour:

Published in.NET CoreC#Entity Framework


  1. Jahan Jahan

    Hi Thomas,
    How can I implement Cascade Delete in a self referencing table?
    (For example there is a Comment Table and man can reply to a comment and this reply comment can reply by someone and consecutive.)

    • Thomas Thomas

      Hey – when I first saw your question my initial guess was perhaps creating a join table so you could have:

      Post (1) —> (M) Comment Replies (M) —> (M) Comment

      Or similar which is how a database might normally handle having many to many relationships which I think (although I haven’t tested) might support cascade delete. However, I did a quick google and see you answered your own question on StackOverflow* with a different approach which looks like it works for you.

      Thanks for asking – nice to see at least one person has seen this post!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.