You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I'm trying to get the EntityFramework.Core extension to write an AuditLog entry per changed property.
For this purpose I've overidden the EntityFrameworkDataProvider.InsertEvent with a custom DataProvider with Issue #195 as inspiration.
The problem is, using DbContextHelper.Core.CreateAuditEvent to create a new EntityFrameworkEvent returns null.
The reason seems to be, at this point in the code execution DbContextHelper.GetModifiedEntries determines all EF Entries have State.Unmodified, even if they are clearly included in the EventEntry changes.
Trying to circumvent CreateAuditEvent by manually creating the contents is impossible due to private/internal properties.
Maybe there is an alternative solution to this problem I'm not seeing, open to all suggestions.
public class AuditLog
{
public int Id { get; set; }
public string Description { get; set; }
public string OldValue { get; set; }
public string NewValue { get; set; }
public string PropertyName { get; set; }
public DateTime AuditDateTime { get; set; }
public Guid? AuditIssuerUserId { get; set; }
public string AuditAction { get; set; }
public string TableName { get; set; }
public int TablePK { get; set; }
}
You don't need to call CreateAuditEvent, you should be able to handle only the Changes property on the original event, like this:
publicoverrideobjectInsertEvent(AuditEventauditEvent){varauditEventEf= auditEvent as AuditEventEntityFramework;if(auditEventEf==null)returnnull;objectresult=null;foreach(var entry in auditEventEf.EntityFrameworkEvent.Entries){if(entry.Changes ==null|| entry.Changes.Count ==0)continue;// Call base.InsertEvent for each changevaroriginalChanges= entry.Changes;foreach(var change in originalChanges){
entry.Changes =newList<EventEntryChange>(){ change };result=base.InsertEvent(auditEvent);}
entry.Changes =originalChanges;}returnresult;}
Notes:
This could impact performance, since it will trigger an insert to the database for each column change.
If you plan to use async calls to DbContext.SaveChangesAsync, you should also implement the InsertEventAsync method on your CustomDataProvider
The Changes property is only available for Updates, so if you also want to audit Inserts and Deletes, you'll need to add the logic to get the column values from the ColumnValues property on the event
Hi,
I'm trying to get the EntityFramework.Core extension to write an AuditLog entry per changed property.
For this purpose I've overidden the
EntityFrameworkDataProvider.InsertEvent
with a custom DataProvider with Issue #195 as inspiration.The problem is, using
DbContextHelper.Core.CreateAuditEvent
to create a new EntityFrameworkEvent returns null.The reason seems to be, at this point in the code execution
DbContextHelper.GetModifiedEntries
determines all EF Entries haveState.Unmodified
, even if they are clearly included in the EventEntry changes.Trying to circumvent CreateAuditEvent by manually creating the contents is impossible due to private/internal properties.
Maybe there is an alternative solution to this problem I'm not seeing, open to all suggestions.
The text was updated successfully, but these errors were encountered: