Reputation: 85
I have solution where project containing data layer information(all models and dbContext) is referenced by windows service project and ASP.NET MVC app since they use the same data structure.
Recently while working on windows service I noticed huge memory usage in debugger when ever there is an issue with constraints but no error was thrown. As I was troubleshooting it I realized that methods SaveChanges()
and SaveChangesAsync()
never throw any type of exception and as result I couldn't catch exception in try/catch block. I can see error "Failed executing DbCommand..." in output window but program would continue to run some loop in DbContext and memory usage would increase at rate of 500MB per second and would rise to maximum available without crashing application.
I managed to record trace log on creating new location entry with non-existent FK, and it seems that after error occurs in dbContext EF starts "Select" querys for every DBSet defined in DbContext and starts tracking for every entity from select query result. You can see that part of log on picture below...
I tried creating new ASP.NET MVC project with EF Core and can't reproduce this issue on clean project so this issue is probably in configuration or something similar.
This whole solution originally started on .NET Framework 4.5 and was later converted(rewritten) to .NET Core 3.1 and finally updated to .NET 5. Data layer was extracted in separate project from MVC project on .NET Core 3.1 version when windows service was needed. I have updated every package to latest version, I tried different configuration options for DbContext and logging solutions but nothing changed. I can reproduce this issue by manually setting wrong ID for constraints on any model so it's not related to specific Model or validation. Unfortunately I can't pin point exact time when this behavior become a thing in solution...
Does anyone know what could be cause of this behavior or where to start troubleshooting it?
Upvotes: 6
Views: 3565
Reputation: 41759
It is a well know issue with Serilog.Exceptions when used with EF Core, see the detailed docs here: https://github.com/RehanSaeed/Serilog.Exceptions#serilogexceptionsentityframeworkcore
Upvotes: 6