In this demo we will examine how to override the SaveChanges method of the DbContext class in order to implement a soft-delete functionality in Entity Framework Core 3.1.
Soft-delete means that, instead of deleting database records, you’ll “mark them” as deleted. This can be accomplished by having some sort of “IsDeleted” column and setting it to true.
We want that anytime we try to delete any entity with the column “IsDeleted”, we instead update that column value to true.
One way of doing this is by overriding the SaveChanges method, like this:
You have to put this code in the class in your project that inherits from DbContext.
So, now if you have an entity or model that have the IsDeleted property, then it will automatically have the soft-delete functionality. Example:
If you don’t want to use soft-delete, then just don’t add an IsDeleted column to your entity.
With soft-delete you don’t delete entities, but update a column indicating that the entity is deleted. So you update instead of delete. You can use this to avoid deleting data that you may want to use later. Override the SaveChanges method to accomplish this as showed in this post.