-
-
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
SaveChangesAsync Infinite Loop when Insert with DbUpdateException #168
Comments
I was not able to reproduce exactly, but I've found a problem and I think that's the cause of the issue. I'm assuming you use the Say you are saving a change on your context. If the change fails (i.e. DbUpdateException) then the framework still tries to save the audit before throwing the exception, but since you use the exact same context for saving audits, the audit saving fails because the context detects the previous change and try to re-apply it. Nevertheless I don't see why you are getting an infinite loop. Fortunately you could workaround it by indicating the framework you want to create a new context instance for each audit operation, with the For example : Audit.Core.Configuration.Setup()
.UseEntityFramework(_ => _
.UseDbContext<YourContext>() // <---- ADD THIS LINE
.AuditTypeExplicitMapper(m => m
.Map<Blog, BlogAudit>()
.Map<Post, PostAudit>()
.AuditEntityAction<IAuditEntity>((ev, entry, entity) =>
{
entity.AuditAction = entry.Action;
entity.Exception = ev.GetEntityFrameworkEvent().ErrorMessage;
}))); I'm wondering if that should be the default behavior... Note: I've added a unit test for this here |
Thanks. It works after applied your workaround above. Based on my observations, after applied the solution,
|
I had the same problem. But, when i used UseDbContext i had another problem. Because i use transactions with System.Transactions on dotnet core 2.2. Two instances of my DbContext throws this exception: "This platform does not support distributed transactions." So, i solved like this:
Configuration.AddCustomAction(ActionType.OnEventSaving, scope =>
{
if (scope.Event.Environment.Exception != null)
{
scope.Discard();
}
});
` |
@penihel for me your way doesn't work, but worked this approach:
|
Describe the bug
SaveChangesAsync Infinite Loop when DbUpdateException.
Only happens in AuditDbContext, DbContext can't reproduce the bug.
To Reproduce
Adding an entry with the same primary key & save changes.
Expected behavior
It should return DbUpdateException just like how It works on DbContext
Libraries (specify the Audit.NET extensions being used including version):
Additional context
I've tried to debug line by line, apparently the code itself loop back to the SaveChangesAsync.
When first received an exception, it went into SaveScopeAsync
Audit.NET/src/Audit.EntityFramework/DbContextHelper.cs
Line 458 in 34280b9
AuditScope.SaveAsync ()
Audit.NET/src/Audit.EntityFramework/DbContextHelper.cs
Line 147 in 34280b9
AuditScope.SaveEventAsync ()
Audit.NET/src/Audit.NET/AuditScope.cs
Line 340 in 929493b
await this._dataProvider.InsertEventAsync(this._event);
Audit.NET/src/Audit.NET/AuditScope.cs
Line 433 in 929493b
await (auditDbContext as IAuditBypass).SaveChangesBypassAuditAsync()
Audit.NET/src/Audit.EntityFramework/Providers/EntityFrameworkDataProvider.cs
Line 161 in 09ca474
int num = await base.SaveChangesAsync();
Audit.NET/src/Audit.EntityFramework/AuditDbContext.cs
Line 196 in f2264fe
step 6 will loop back to the SaveChangesAsync again & formed an infinite loop.
The text was updated successfully, but these errors were encountered: