2
Vote

more than 1 SubmitChanges() can cause multiple audit entries

description

Doddle overrides SubmitChanges() on the datacontext and, within here, calls DefaultAuditDefintions() which then calls user code which sets up the audit definitions.

If you call SubmitChanges() more than once, then DefaultAuditDefinitions() will be called each time, adding to the list of audit definitions. During the creation of the audit records, the code loops through audit definitions and adds a audit record for each matching definition found. The bug won't occur if the audit records are adding during the first SubmitChanges().

Multiple calls to SubmitChanges() is valid in case you want to get some primary keys back, or to have LINQ populate the entity relationships. Doddle does it itself.

To reproduce:
  1. create the data context.
  2. Add some new records -- these don't have to be tracked (defined). In fact, this step isn't even necessary.
  3. Call SubmitChanges(). (If you loop this call and call 10 times, you should have 11 audit entries after step 5.)
  4. Create some new records and add to the context. (I'm using the primary keys from step #2, but that shouldn't be necessary.)
  5. Call SubmitChanges(). You should be able to watch in the AuditProcessor.cs as it calls _context.InsertAuditRecord(record); twice.
To fix, I replaced:

DefaultAuditDefinitions();

with the following code on line 134 of AuditDataContext.cs:

if (_auditDefinitions.Count == 0)
        {
            DefaultAuditDefinitions();
        }
James

comments

tecGoblin wrote Aug 10, 2009 at 8:10 AM

This seems like a solution! I tried something very similar (clearing the auditdefinitions before defaulting them) in the same line of code, and it worked too.

mhidinger wrote Dec 16, 2009 at 3:17 PM

Hi James,

Thanks for reporting this issue and posting a fix. I will incorporate a version of this fix into the next release which should be within a week or so.

-Matt

the_real_fonzy wrote Jun 9, 2011 at 3:13 PM

I also added _queuedRecords.Clear(); at the end of SubmitChanges so it doesn't keep adding the same audit records.

the_real_fonzy wrote Jun 9, 2011 at 3:14 PM

I also added _queuedRecords.Clear(); to the end of SubmitChanges to prevent duplicate audit records.