My previous post explained how to setup soft deletes for queries automatically. This time, I’m going to talk about the other part: actually replacing a delete for an update that sets the is deleted column.
The key here is to intercept the SaveChanges method, find out all entities that are ISoftDeletable and are marked for deletion and then set their IsDeleted property and change their state to be modified instead of deleted:
public override int SaveChanges()
foreach (var entry in this.ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted))
if (entry.Entity is ISoftDeletable)
entry.Property(_isDeletedProperty).CurrentValue = true;
entry.State = EntityState.Modified;