DbContext ChangeTracker: identifier of the added entity for audit

I have something like this:

public override int SaveChanges()
{
    foreach (var changeLog in this.ChangeTracker.Entries()
        .Where(p => p.State == EntityState.Added ||
               p.State == EntityState.Deleted ||
               p.State == EntityState.Modified)
        .SelectMany(entity => AuditRecords(entity)))
    {
        this.ChangeLogs.Add(changeLog);
    }

    return base.SaveChanges();
}

But, of course, checked change logs will not contain the primary key value for the object when adding an EntityState (before SaveChanges). How can I get the primary key value for audit change?

Richard

+5
source share
2 answers

I would save references to objects in List<T>(or an array or other such structure), and then do a call to the base implementation SaveChanges.

, ( , , ), , .

, , Entity Entity ( , ChangeLogs); . , , .

+5

GUID , .

0

All Articles