-
-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple Tracking Changes #487
Comments
Please add more context. Are you auditing changes on EF using Audit.EntityFramework? |
Sorry, I am using Here is my Configs I am current using:
What I am trying to do is to add an extra step, to set |
You can access the actual entity being modified from the audit action with Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.AuditTypeMapper(t => typeof(AuditTrail))
.AuditEntityAction<AuditTrail>((ev, entry, entity) =>
{
if (entry.Entity is AuditEntity auditEntity)
{
entity.ModifiedDate = auditEntity.ModifiedDate;
}
.... |
Change to : Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.AuditTypeMapper(t => typeof(AuditTrail))
.AuditEntityAction<AuditTrail>((ev, entry, entity) =>
{
if (entry.GetEntry().Entity is AuditEntity auditEntity)
{
entity.ModifiedDate = auditEntity.ModifiedDate;
}
.... And Worked .. thanks a lot !! |
Actually .. doing this way... this became an infinity loop... Like, if I insert a new Item, next will be the Update triggered by my changes to Entity.. and so on... |
You need set IncludeEntityObjects setting to Also try decorating with |
Nice, let's double check.. if I am doing something wrong. Now I have Have decorated those: [AuditIgnore]
public class AuditTrail { ... }
[AuditIgnore]
public abstract class AuditEntity { ... } But still having infinity loop :/ |
I'm confused regarding your entities and configuration... If you could create a minimal project that reproduces the issue I would be able to help Seems like you are using the same |
If that's the case (AppDbContext not inheriting from AuditDbContext), another option is to implement IAuditBypass on your DbContext: public class AppDbContext : DbContext, IAuditBypass
{
public int SaveChangesBypassAudit()
{
return base.SaveChanges():
}
public Task<int> SaveChangesBypassAuditAsync()
{
return base.SaveChangesAsync();
}
// ...
} In that way, any Audit entity will bypass the audited SaveChanges, and call the original DbContext's SaveChanges |
Created this sample project: https://github.com/EliveltonRepolho/Audit.NET.Trail.Simple.Logs You can check the infinity loop here: https://github.com/EliveltonRepolho/Audit.NET.Trail.Simple.Logs/blob/main/MiniDemo/Program.cs#L37 And thanks a lot for your attention ! |
So you are trying to modify the actual entity, not the audit entity. But the Audit Entity Action takes place after the entity is modified and saved, so you cannot update the actual entity in the Audit Entity Action. The I still don't get the But, I think you can still do what you want at the AuditEvent level, from a Custom Action of type OnScopeCreated, since the scope is created before the SaveChanges takes place: Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
var efEvent = scope.GetEntityFrameworkEvent();
foreach (var entry in efEvent.Entries)
{
var now = DateTime.UtcNow;
if (entry.GetEntry().Entity is AuditEntity auditEntity)
{
if (entry.Action == "Insert")
{
auditEntity.CreatedDate = now;
}
auditEntity.ModifiedDate = now;
}
}
}); |
Humm .. undestood now.
Will try your suggestion today.. Thanks a lot |
Just one thing about using this solution, I am still testing and reading the docs if has another config the must be done. But in the example I sent (https://github.com/EliveltonRepolho/Audit.NET.Trail.Simple.Logs)... I updated with your suggestion, but if for example I try to insert same Employee ID (Forcing violating Primary Key) we have an infinity loop now in this portion of code :/ |
The Audit.EntityFramework.Core will trigger the audit even if the original SaveChanges fails. |
Also, the recommendation is to set up a different instance of your DbContext for the audit entities by calling Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.UseDbContext<AppDbContext>()
...); Check this #168 (comment) |
thanks for the recommendation too, I've implemented and now everything looks good. Closing this issue .. will leave this example available if might be useful for someone else. Thanks a lot for helping with it ! |
I have a question about it if is possible to track like only Date and User Created/Modified for example, inside classes.. Not creating a full history change.. example:
I did not find something related if this is possible to do using this
Audit.NET
...My use case is:
I should create a Trail Table, which I am using this
Audit.NET
to do it, but do want to setDate and User Created/Modified
for some entities, so this way it is easy to get.The text was updated successfully, but these errors were encountered: