Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soft Deletes ( IsHistorical column ) with EntityFramework

I'm working with a database where the designers decided to mark every table with a IsHistorical bit column. There is no consideration for proper modeling and there is no way I can change the schema.

This is causing some friction when developing CRUD screens that interact with navigation properties. I cannot simply take a Product and then edit its EntityCollection I have to manually write IsHistorical checks all over the place and its driving me mad.

Additions are also horrible because so far I've written all manual checks to see if an addition is just soft deleted so instead of adding a duplicate entity I can just toggle IsHistoric.

The three options I've considered are:

  1. Modifying the t4 templates to include IsHistorical checks and synchronization.

  2. Intercept deletions and additions in the ObjectContext, toggle the IsHistorical column, and then synch the object state.

  3. Subscribe to the AssociationChanged event and toggle the IsHistorical column there.

Does anybody have any experience with this or could recommend the most painless approach?

Note: Yes, I know, this is bad modeling. I've read the same articles about soft deletes that you have. It stinks I have to deal with this requirement but I do. I just want the most painless method of dealing with soft deletes without writing the same code for every navigation property in my database.

Note #2 LukeLed's answer is technically correct although forces you into a really bad poor mans ORM, graph-less, pattern. The problem lies in the fact that now I'm required to rip out all the "deleted" objects from the graph and then call the Delete method over each one. Thats not really going to save me that much manual ceremonial coding. Instead of writing manual IsHistoric checks now I'm gathering deleted objects and looping through them.

like image 670
John Farrell Avatar asked Dec 21 '09 20:12

John Farrell


People also ask

How do I soft delete in Entity Framework?

The Soft Delete feature allows you to flag entities as deleted (Soft Delete) instead of deleting them physically (Hard Delete). The soft delete feature can be achieved by using the 'IEFSoftDelete' interface. By default, this interface is always added to the manager.

What is difference between delete and soft delete?

Hard vs soft deletesA “hard” delete is when rows are deleted using DELETE FROM table WHERE ... A “soft” delete is when rows are deleted using UPDATE table SET deleted_at = now() WHERE ...

What is meant by soft delete?

soft deletion (plural soft deletions) (databases) An operation in which a flag is used to mark data as unusable, without erasing the data itself from the database.

How does soft delete work?

What Is Soft Delete? Soft delete performs an update process to mark some data as deleted instead of physically deleting it from a table in the database. A common way to implement soft delete is to add a field that will indicate whether data has been deleted or not.


1 Answers

I am using generic repository in my code. You could do it like:

public class Repository<T> : IRepository<T> where T : EntityObject
{
    public void Delete(T obj)
    {
        if (obj is ISoftDelete)
            ((ISoftDelete)obj).IsHistorical = true
        else
            _ctx.DeleteObject(obj);
    }

Your List() method would filter by IsHistorical too.

EDIT:

ISoftDelete interface:

public interface ISoftDelete
{
    bool IsHistorical { get; set; }
}

Entity classes can be easily marked as ISoftDelete, because they are partial. Partial class definition needs to be added in separate file:

public partial class MyClass : EntityObject, ISoftDelete
{

}
like image 97
LukLed Avatar answered Sep 23 '22 12:09

LukLed