5
Vote

Update auditing mixes Current and Original values

description

Not sure how I hadn't noticed this until today, but the original and current values are getting reversed for updates within LINQ.

From AuditableDataContext:

return
            table.GetModifiedMembers(entity).Select(
                mmi =>
                new MemberAudit(mmi.Member, mmi.CurrentValue, mmi.OriginalValue));
From MemberAudit:

public MemberAudit(MemberInfo mi, object originalValue, object currentValue)
    {
        Member = mi;
        OriginalValue = originalValue;
        CurrentValue = currentValue;
    }
Note the reversal in the constructor.

comments

CombatWombat wrote Dec 14, 2009 at 7:11 AM

I just found this too after creating some unit/functional tests. For those not building from source the work-around is pretty simple, just change AuditDataContext.InsertAuditRecordToDatabase

foreach (var modifiedField in entity.ModifiedFields)
{
...
field.OldValue = entity.Action == AuditAction.Update ? modifiedField.NewValue : modifiedField.OldValue;
field.OldValue = entity.Action == AuditAction.Update ? modifiedField.OldValue : modifiedField.NewValue;
...
}

Unit tests FTW!

CombatWombat wrote Dec 14, 2009 at 7:11 AM

I just found this too after creating some unit/functional tests. For those not building from source the work-around is pretty simple, just change AuditDataContext.InsertAuditRecordToDatabase

foreach (var modifiedField in entity.ModifiedFields)
{
...
field.OldValue = entity.Action == AuditAction.Update ? modifiedField.NewValue : modifiedField.OldValue;
field.OldValue = entity.Action == AuditAction.Update ? modifiedField.OldValue : modifiedField.NewValue;
...
}

Unit tests FTW!

mhidinger wrote Dec 16, 2009 at 4:21 PM

Hello,

Thanks to both of you for recording and resolving this issue. It has been fixed in the upcoming release which should be ready in a week or so.

CombatWombat has inspired me to really overhaul the internal auditing implementation so I can support many more fine-grained unit tests which will catch this type of regression in the future.

-Matt