StackOverflow exception on SaveChanges if DbEntityValidation exception is thrown
See original GitHub issueI’m running into stackoverflow exception when trying to call saveChanges and hitting a DbEntityValidationException. DbContextHelper calls SaveScopeAsync in the exception handler, this goes all the way to InsertEventAsync being called and starting the infinite recursion.
Libraries
- Audit…NET: 15.1.0
- Audit.EntityFramework: 15.1.0
- EntityFramework 6.3.0
StackTrace
AuditDbContext.Audit.EntityFramework.IAuditBypass.SaveChangesBypassAuditAsync() [4]
async EntityFrameworkDataProvider.InsertEventAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\19\87f461ad\EntityFrameworkDataProvider.cs:line 180 [4]
AsyncTaskMethodBuilder<object>.Start<Audit.EntityFramework.Providers.EntityFrameworkDataProvider.<InsertEventAsync>d__19>() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\A92FC6C4-7B95-420B-B8F0-A07FCB960BD3\ac\87838bd3\AsyncTaskMethodBuilder'1.cs:line 50 [4]
EntityFrameworkDataProvider.InsertEventAsync() [4]
async AuditScope.SaveEventAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\50EFBDC2-3648-43DA-BE8B-EBC17F4C94FC\b2\60910ea1\AuditScope.cs:line 367 [4]
AsyncTaskMethodBuilder.Start<Audit.Core.AuditScope.<SaveEventAsync>d__41>() [4]
AuditScope.SaveEventAsync() [4]
async AuditScope.SaveAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\50EFBDC2-3648-43DA-BE8B-EBC17F4C94FC\b2\60910ea1\AuditScope.cs:line 306 [4]
AsyncTaskMethodBuilder.Start<Audit.Core.AuditScope.<SaveAsync>d__35>() [4]
AuditScope.SaveAsync() [4]
async DbContextHelper.SaveScopeAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\9b\27887c50\DbContextHelper.cs:line 211 [4]
AsyncTaskMethodBuilder.Start<Audit.EntityFramework.DbContextHelper.<SaveScopeAsync>d__9>() [4]
DbContextHelper.SaveScopeAsync() [4]
async DbContextHelper.SaveChangesAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\9b\27887c50\DbContextHelper.cs:line 419 [4]
AsyncTaskMethodBuilder<int>.Start<Audit.EntityFramework.DbContextHelper.<SaveChangesAsync>d__22>() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\A92FC6C4-7B95-420B-B8F0-A07FCB960BD3\ac\87838bd3\AsyncTaskMethodBuilder'1.cs:line 50 [4]
DbContextHelper.SaveChangesAsync() [4]
AuditDbContext.SaveChangesAsync() [4]
DbContext.SaveChangesAsync() [4]
AuditDbContext.Audit.EntityFramework.IAuditBypass.SaveChangesBypassAuditAsync() [3]
async EntityFrameworkDataProvider.InsertEventAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\19\87f461ad\EntityFrameworkDataProvider.cs:line 180 [3]
AsyncTaskMethodBuilder<object>.Start<Audit.EntityFramework.Providers.EntityFrameworkDataProvider.<InsertEventAsync>d__19>() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\A92FC6C4-7B95-420B-B8F0-A07FCB960BD3\ac\87838bd3\AsyncTaskMethodBuilder'1.cs:line 50 [3]
EntityFrameworkDataProvider.InsertEventAsync() [3]
async AuditScope.SaveEventAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\50EFBDC2-3648-43DA-BE8B-EBC17F4C94FC\b2\60910ea1\AuditScope.cs:line 367 [3]
AsyncTaskMethodBuilder.Start<Audit.Core.AuditScope.<SaveEventAsync>d__41>() [3]
AuditScope.SaveEventAsync() [3]
async AuditScope.SaveAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\50EFBDC2-3648-43DA-BE8B-EBC17F4C94FC\b2\60910ea1\AuditScope.cs:line 306 [3]
AsyncTaskMethodBuilder.Start<Audit.Core.AuditScope.<SaveAsync>d__35>() [3]
AuditScope.SaveAsync() [3]
async DbContextHelper.SaveScopeAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\9b\27887c50\DbContextHelper.cs:line 211 [3]
AsyncTaskMethodBuilder.Start<Audit.EntityFramework.DbContextHelper.<SaveScopeAsync>d__9>() [3]
DbContextHelper.SaveScopeAsync() [3]
async DbContextHelper.SaveChangesAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\9b\27887c50\DbContextHelper.cs:line 419 [3]
AsyncTaskMethodBuilder<int>.Start<Audit.EntityFramework.DbContextHelper.<SaveChangesAsync>d__22>() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\A92FC6C4-7B95-420B-B8F0-A07FCB960BD3\ac\87838bd3\AsyncTaskMethodBuilder'1.cs:line 50 [3]
DbContextHelper.SaveChangesAsync() [3]
AuditDbContext.SaveChangesAsync() [3]
DbContext.SaveChangesAsync() [3]
AuditDbContext.Audit.EntityFramework.IAuditBypass.SaveChangesBypassAuditAsync() [2]
async EntityFrameworkDataProvider.InsertEventAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\19\87f461ad\EntityFrameworkDataProvider.cs:line 180 [2]
AsyncTaskMethodBuilder<object>.Start<Audit.EntityFramework.Providers.EntityFrameworkDataProvider.<InsertEventAsync>d__19>() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\A92FC6C4-7B95-420B-B8F0-A07FCB960BD3\ac\87838bd3\AsyncTaskMethodBuilder'1.cs:line 50 [2]
EntityFrameworkDataProvider.InsertEventAsync() [2]
async AuditScope.SaveEventAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\50EFBDC2-3648-43DA-BE8B-EBC17F4C94FC\b2\60910ea1\AuditScope.cs:line 367 [2]
AsyncTaskMethodBuilder.Start<Audit.Core.AuditScope.<SaveEventAsync>d__41>() [2]
AuditScope.SaveEventAsync() [2]
async AuditScope.SaveAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\50EFBDC2-3648-43DA-BE8B-EBC17F4C94FC\b2\60910ea1\AuditScope.cs:line 306 [2]
AsyncTaskMethodBuilder.Start<Audit.Core.AuditScope.<SaveAsync>d__35>() [2]
AuditScope.SaveAsync() [2]
async DbContextHelper.SaveScopeAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\9b\27887c50\DbContextHelper.cs:line 211 [2]
AsyncTaskMethodBuilder.Start<Audit.EntityFramework.DbContextHelper.<SaveScopeAsync>d__9>() [2]
DbContextHelper.SaveScopeAsync() [2]
async DbContextHelper.SaveChangesAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\9b\27887c50\DbContextHelper.cs:line 419 [2]
AsyncTaskMethodBuilder<int>.Start<Audit.EntityFramework.DbContextHelper.<SaveChangesAsync>d__22>() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\A92FC6C4-7B95-420B-B8F0-A07FCB960BD3\ac\87838bd3\AsyncTaskMethodBuilder'1.cs:line 50 [2]
DbContextHelper.SaveChangesAsync() [2]
AuditDbContext.SaveChangesAsync() [2]
DbContext.SaveChangesAsync() [2]
AuditDbContext.Audit.EntityFramework.IAuditBypass.SaveChangesBypassAuditAsync() [1]
async EntityFrameworkDataProvider.InsertEventAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\19\87f461ad\EntityFrameworkDataProvider.cs:line 180 [1]
AsyncTaskMethodBuilder<object>.Start<Audit.EntityFramework.Providers.EntityFrameworkDataProvider.<InsertEventAsync>d__19>() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\A92FC6C4-7B95-420B-B8F0-A07FCB960BD3\ac\87838bd3\AsyncTaskMethodBuilder'1.cs:line 50 [1]
EntityFrameworkDataProvider.InsertEventAsync() [1]
async AuditScope.SaveEventAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\50EFBDC2-3648-43DA-BE8B-EBC17F4C94FC\b2\60910ea1\AuditScope.cs:line 367 [1]
AsyncTaskMethodBuilder.Start<Audit.Core.AuditScope.<SaveEventAsync>d__41>() [1]
AuditScope.SaveEventAsync() [1]
async AuditScope.SaveAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\50EFBDC2-3648-43DA-BE8B-EBC17F4C94FC\b2\60910ea1\AuditScope.cs:line 306 [1]
AsyncTaskMethodBuilder.Start<Audit.Core.AuditScope.<SaveAsync>d__35>() [1]
AuditScope.SaveAsync() [1]
async DbContextHelper.SaveScopeAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\9b\27887c50\DbContextHelper.cs:line 211 [1]
AsyncTaskMethodBuilder.Start<Audit.EntityFramework.DbContextHelper.<SaveScopeAsync>d__9>() [1]
DbContextHelper.SaveScopeAsync() [1]
async DbContextHelper.SaveChangesAsync() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\26A06B52-74B7-478B-A879-C62DAC817477\9b\27887c50\DbContextHelper.cs:line 419 [1]
AsyncTaskMethodBuilder<int>.Start<Audit.EntityFramework.DbContextHelper.<SaveChangesAsync>d__22>() at C:\Users\jakub\.Rider2019.3\config\resharper-host\DecompilerCache\decompiler\A92FC6C4-7B95-420B-B8F0-A07FCB960BD3\ac\87838bd3\AsyncTaskMethodBuilder'1.cs:line 50 [1]
DbContextHelper.SaveChangesAsync() [1]
AuditDbContext.SaveChangesAsync() [1]
DbContext.SaveChangesAsync() [1]
Context
public class EaContext : AuditDbContext, IEaContext
{
public EaContext(string connectionString) : base(connectionString)
{
Configuration.LazyLoadingEnabled = false;
Mode = AuditOptionMode.OptOut;
Audit.Core.Configuration.Setup()
.UseEntityFramework(ef => ef
.AuditTypeExplicitMapper(m => m
.Map<Car, CarHistory>((source, audit) => { audit.EntityId = source.Id; })
.AuditEntityAction<IAudit>((evt, entry, auditEntity) =>
{
auditEntity.AuditDate = DateTime.UtcNow;
auditEntity.UserName = "sampleUser";
auditEntity.Action = entry.Action; // Insert, Update, Delete
})
)
);
}
}
When looking at the library, i found what I suspect is an incorrect call to this.SaveChanges instead of base.SaveChanges in AuditDbContextSaveChangesBypassAuditAsync()
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
EF6 Throws Exception for EntityValidationErrors When I Try ...
I think that it should be okay because I just get an entity object and just change its properties and save changes. Is...
Read more >DbEntityValidationException Class (System.Data.Entity. ...
Exception thrown from SaveChanges() when validating entities fails. ... Initializes a new instance of DbEntityValidationException.
Read more >Easy way to improve DbEntityValidationException
DbEntityValidationException is thrown by the SaveChanges method which resides on the DbContext class. Fortunately, you can override the default ...
Read more >Easy way to improve DbEntityValidationException of Entity ...
DbEntityValidationException is thrown by the SaveChanges method which resides on the DbContext class. Fortunately, you can override the default ...
Read more >AbpException vs EfCoreException. #1095 - ABP Commercial
This is EF Core default behaviour. When there's DbUpdateException, the default exception handler only handles An internal error occurred during your request ....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s it. Just upgraded and the exception is raised correctly. Thanks a ton man!
This was fixed on version 15.1.1, please upgrade your references and re-test.