Insert\Update - object values history maintenance in ObjectContext

393 views Asked by At

I have an windows service which processes input from xml files. I need to insert new records and update the existing records every time I get a new file. I now need to implement insert\update history every time after an operation has occurred. I am required to maintain this in a separate table by displaying old value and new value. Is there any existing methodologies or techniques available for implementing this in easier way i.e something like comparing two objects and identifying modified fields. Please provide any suggestions. I am using Entityframework 5.0 and sql 2012.

3

There are 3 answers

0
Simon Taylor On

Specifically for EF, you can override DbContext.SaveChanges() and iterate over DbContext.ChangeTracker.Entries(). Each entry contains the current and original values for the properties of the entity.

0
Anant Laxmikant Bobde On

There are multiple ways to do this.

  1. Using interceptors of your persistence API framework. For eg JPA or Hibernate framework provides facade around your entity operations which runs after DML operations in database.

  2. Event Listeners: You should be able to create event listeners inside your persistence framework which will be triggered and insert history data in your history tables after each DML operation.

  3. Database triggers: This is indeed one of the most simplest way of maintaining history information for a given row/table.

Hope these pointers help Anant

0
Darey On

My Entity class was derived from ObjectContext, which did not provide ChangeTracker for obtaining the modified values. However, for ObjectContext, we can obtain the modified values using DBContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified) You can use EntityState.Deleted, EntityState.Added if required. The below is the sample Implementation

     Entities DBContext = new Entities();

        var d = DBContext.StudentTableName.Where(x => x.stname == "Stock").FirstOrDefault();
        if(d!= null)
        {
        d.Id = "345";
        DBContext.StudentTableName.ApplyCurrentValues(d);

        //Need to Include Audit Logging before  save, or can override save function.
        var entrList = DBContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
        foreach (var stateEntry in entrList)
        {
            var currentValues = stateEntry.CurrentValues;
            var originalValues = stateEntry.OriginalValues;
            var modifiedProperties = stateEntry.GetModifiedProperties();
            foreach (string modifiedProperty in modifiedProperties)
            {
                var currentValue = currentValues.GetValue(currentValues.GetOrdinal(modifiedProperty));
                var originalValue = originalValues.GetValue(originalValues.GetOrdinal(modifiedProperty));
                if (!originalValue.Equals(currentValue))
                {
                    //Perform the logging operation
                }
            }
        }
        // Audit Logging Performed
        DBContext.SaveChanges();
}