Getting changes in an object in Entity Framework

From: stackOverflow

if (change.State == System.Data.EntityState.Modified) {
     // Log Modified
     var item = change.Cast<IEntity>().Entity;
     var originalValues = this.Entry(item).OriginalValues;
     var currentValues = this.Entry(item).CurrentValues;

     foreach (string propertyName in originalValues.PropertyNames) {
         var original = originalValues[propertyName];
         var current = currentValues[propertyName];
         if (!Equals(original, current)) {
            // log propertyName: original --> current
         }
     }
}

Advertisements

Leave a Reply