0

I have a WPF Application with Entity Data Model.

I would like to set IsDeleted flag to true when entity is deleted instead of deleting from database.

I read many thread here and alot of threads are suggesting to use Modification Function Mapping with the use of conditional mapping.

I used conditional mapping for filtering IsDeleted = true rows and works great.

However, While I'd like to use Modification Function Mapping for just Delete Function, the program give me error, trying to find Function for Insert and Update as well on SaveChanges() method.

I only specified for Delete Function. Is there a way to use only Delete Modification Function and not all Insert, Update, Delete?

If not, is there any other ways that can achieve this? P.S. I read about using Instead Of Delete trigger. However, I would like to avoid it, if possible, since we are developing the program for a mobile application and triggers might slow down the application.

Thank you in advance!

Chikakow
  • 29
  • 6
  • is the IsDeleted a database column, or just an application concept? – Kyeotic Apr 26 '12 at 21:05
  • They are database columns. However, I'm not mapping them to properties of entities in Entity Data Model but I'm using them for conditional mappings in each entities in EDM to filter out deleted items. Thanks – Chikakow Apr 27 '12 at 13:43
  • Related thread is here. http://stackoverflow.com/questions/7430286/how-do-i-override-deleteobject-in-entity-framework Ladislav is talking about Modification Function Mapping here. That's what I'm having trouble with. Any help would be great. Thanks! – Chikakow Apr 27 '12 at 13:49

1 Answers1

1

We implemented a soft/logical delete in our Entity Framework 4.1 solution.

I partially described it in this answer: Partial Answer

First, we added an IsDeleted column to all the tables in the DB that require a soft, or logical delete. In the template, I added inheritance to the IEnforceLogicalDelete interface

public interface IEnforceLogicalDelete
    {
        global::System.Boolean IsDeleted{ get; set; }
    }

In the template where the entities are being defined we added this line:

if(entity.Properties.Where(p => p.DeclaringType == entity && (p.Name == "IsDeleted")).ToList().Count == 1){#>, IEnforceLogicalDelete<#} 

This adds the interface after the entity inherits from EntityObject.

Once this is set up, it is time to either create your own save method on the Context, which is what we did, or in the Context.OnSavingChanges event.

As I said above we created our own method (SaveChangesWithHistory) where we also logged all changes (inserts, updates, and deletes) into a history table.

To do the logical delete you need to capture all of the state entries that have an EntityState of Deleted, change their state to Modified, and set your flag. Implementing from the interface makes it easy to do a full delete on any entity that needs it.

foreach (ObjectStateEntry entry in this.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Deleted | EntityState.Added))
        {

            if (entry.State == EntityState.Deleted)
            {
               if (entry.Entity is IEnforceLogicalDelete)
                    {
                        IEnforceLogicalDelete delete = entry.Entity as IEnforceLogicalDelete;
                        entry.ChangeState(EntityState.Modified);
                        entry.ApplyOriginalValues(delete);
                        delete.IsDeleted = true;
                    }
                 }

}

Community
  • 1
  • 1
Noel
  • 600
  • 16
  • 37