Tuesday, July 7, 2020

Entity Framework Core - Soft delete

The easiest way to implement soft delete in EF Core is through query filters.

This filter can be specified when creating our EF Core model:

Now every time when query for the ‘Role’ object an extra ‘WHERE’ clause is included that filters with IsDeleted=false.

But hold your horses we are not there yet, we also have to override our SaveChanges()and SaveChangesAsyc() methods on the DbContext otherwise the ‘Role’ entity will still be removed from the database when we call Remove().

Remark: As a possible improvement you could generate a base class or interface for all ‘soft deletable’ entities.