I'm using this code block to save Audit information to a Detail table. During this save, I need to get the PK from the AuditLog table record that was just saved so I can add the Detail record.
Audit.Core.Configuration.AddCustomAction(ActionType.OnEventSaved, scope =>
{
auditService.ConfigureAuditDetail(scope);
});
Since I only have scope available I am unable to locate the AuditLog entity except by a lookup against the context. Is this safe or could another record be saved before I get the right PK.
ctx.AuditLog.OrderByDescending(x => x.Id).FirstOrDefault().Id;
Is there any way to do this better?
public void ConfigureAuditDetail(AuditScope scope)
{
var efEvent = (scope.Event as AuditEventEntityFramework)?.EntityFrameworkEvent;
List<EventEntryChange> currentChanges = new List<EventEntryChange>();
var ctx = efEvent.GetDbContext() as DbContext;
var auditLogId = ctx.AuditLog.OrderByDescending(x => x.Id).FirstOrDefault().Id;
}