Reputation: 339
I Have 2 Audit tables one is Audit_ProfileFan and second is Audit_StatusChanges
The first table AuditProfileFan
should audit every time update or insert has been made to ProfileFan Table.
The second Audit_StatusChanges should only audit when an update of a certain column FanStatusId is made.
Audit.Core.Configuration.Setup() .UseEntityFramework(ef => ef.AuditTypeExplicitMapper(m => m
.Map<FanActivity, Audit_FanActivity>((fanActivity, auditFanActivity) =>
{
auditFanActivity.ProfileFanId = fanActivity.ProfileFanId;
auditFanActivity.ActivityId = auditFanActivity.ActivityId;
})
.Map<DeliveryActions, Audit_DeliveryActions>((deliveryAction, auditDeliveryAction) =>
{
auditDeliveryAction.ProfileFanId = deliveryAction.FanId;
auditDeliveryAction.DeliveryActionId = deliveryAction.DeliveryActionId;
})
.Map<Fan, Audit_Fan>()
.Map<ProfileFan, Audit_StatusChanges>((profileFan, auditStatusChanges) =>
{
auditStatusChanges.ProfileFanId = profileFan.Id;
//auditStatusChanges.OriginalValue = profileFan.FanStatusId;
//auditStatusChanges.NewValue = profileFan.FanStatusId;
})
.Map<ProfileFan, Audit_ProfileFan>((profileFan, auditProfileFan) =>
{
auditProfileFan.ProfileFanId = profileFan.Id;
auditProfileFan.FanId = profileFan.FanId;
auditProfileFan.EmailResponseStatusId = profileFan.EmailResponseStatusId;
auditProfileFan.FanStatusId = profileFan.FanStatusId;
})
.Map<TagFan, Audit_TagFan>((tagFan, auditTagFan) =>
{
auditTagFan.ProfileFanId = tagFan.ProfileFanId;
auditTagFan.TagId = tagFan.TagId;
})
.AuditEntityAction<IAuditLog>((evt, entry, auditEntity) =>
{
if(entry.Table=="ProfileFan" && entry.Action=="Update")
{
//auditEntity.OriginalValue = profileFan.FanStatusId;
//auditEntity.NewValue = profileFan.FanStatusId;
}
auditEntity.AuditDate = DateTime.Now;
auditEntity.AuditUser = evt.Environment.UserName;
auditEntity.Action = entry.Action; // Insert, Update, Delete
auditEntity.AuditUsername = HttpContext.Current.User.Identity.Name;
})
)
);
But every time an update is made it audits only one table in this case Audit_ProfileFan.
Is my requirement possible or should I do some kind of workaround?
Upvotes: 2
Views: 726
Reputation: 13114
This is not possible with the current version of the EntityFramework data provider, since you can only map from the known entity type and nothing else.
But I've found a way to allow that kind of use cases with minimal impact, by adding a new Map<T>
overload that lets you specify the final audit type as a function of the EventEntry, so you would be able to map the same input data type to multiple output audit types, depending on the modified entry.
So for example you could map ProfileFan
to different tables depending on the SQL operation (insert/update), with something like this:
Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef.AuditTypeExplicitMapper(m => m
.Map<ProfileFan>(
mapper: entry => entry.Action == "Insert" ? typeof(Audit_ProfileFan) : typeof(Audit_StatusChanges),
entityAction: (ev, entry, entity) =>
{
if (entity is Audit_ProfileFan pf)
{
// action for profile fan
// pf.xxxx = ...;
}
else if (entity is Audit_StatusChanges ss)
{
// action for status changes
// ss.xxxx = ...;
}
})
.Map<TagFan, Audit_TagFan>(/*...*/)
.AuditEntityAction<IAuditLog>((evt, entry, auditEntity) =>
{
// common action...
})));
This will be released soon, here is the commit with the changes.
This is included on Audit.EntityFramework
library starting on version 14.6.2
Upvotes: 2