0

I want to log the differences when a user updates an Entity Framework Object (any differences in the whole object graph). I'm a stuck with using version 5 of EF in this particular project for now.

For Each Say it is a customer with a list of orders. I want to output what has changed in a customer field (e.g. like total number or orders changed from 4 to 5). Then output what changed in each order (like order one status changed from ordered to delivered, and order 5 was added).

So Log would look something like this Customer Modified ID =5, Field TotalNumberOfOrders: Orginal Value 4, New Value 5 Customer.Order Added ID = 10 Customer.Order Modified Field Quanity: Origanl Value = 15, New Value = 2

I'm just going to log these values to disk for some diagnostics.

DermFrench
  • 3,698
  • 7
  • 40
  • 68
  • 1
    Take a look at This http://stackoverflow.com/questions/6867459/implementing-audit-log-change-history-with-mvc-entity-framework – Morten Anderson May 16 '14 at 14:06

1 Answers1

0

You can register for OnSavingChanges() callback function which gets called while saving changes to db. Later you an log original/modified fields for auditing propose.

void OnSavingChanges(object sender, EventArgs e)
    {
        var modifiedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
        foreach (var entry in modifiedEntities)
        {
            var modifiedProps = ObjectStateManager.GetObjectStateEntry(entry.EntityKey).GetModifiedProperties();
            var currentValues = ObjectStateManager.GetObjectStateEntry(entry.EntityKey).CurrentValues;
            //iterate and save changes to log for auditing.
        }
    }
shivakumar
  • 2,934
  • 15
  • 28