How to dispose created DbContext in EntityFrameworkDataProvider?
See original GitHub issue Audit.Core.Configuration.Setup().UseFactory(() => new EntityFrameworkDataProvider()
{
DbContextBuilder = ev => applicationBuilder.ApplicationServices.CreateScope().ServiceProvider.GetRequiredService<WebDbContext>(), // this line
ExplicitMapper = ee => typeof(AuditEntry),
AuditEntityAction = (evt, entry, auditEntity) =>
{
var a = (AuditEntry)auditEntity;
a.Id = 0;
a.AuditDate = DateTime.UtcNow;
a.EntityType = entry.Table;
return Task.FromResult(true); // return false to ignore the audit
}
});
Please take the above code into consideration. Since this construction code will be called every time when a high level auditing take place. Without automatically disposal of such DbContext, it will lead to idle connection. May I know that how to control the lifetime of the connection?
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
c# - Dispose of entity objects created by DbContext
We use these entities only in DAL layer. Repositories convert them to models, which are returned outside. So, the entities could be disposed...
Read more >Best Practices in Using the DbContext in EF Core
Avoid Disposing DbContext Instances. It would help if you did not dispose of DbContext objects in most cases. Although the DbContext implements ...
Read more >DbContext.Dispose Method (System.Data.Entity)
Disposes the context. The underlying ObjectContext is also disposed if it was created is by this context or ownership was passed to this...
Read more >DbContext Lifetime, Configuration, and Initialization
It is very important to dispose the DbContext after use. This ensures both that any unmanaged resources are freed, and that any events...
Read more >Is it OK to create an Entity Framework DataContext object ...
Although the DbContext implements IDisposable , you shouldn't manually dispose it, nor should you wrap it in a using statement. DbContext ...
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
@thepirat000 Thank you so much for your prompt action. The most efficient maintainer I have ever seen.
The new
DisposeDbContext
setting was added on version 20.0.3, please upgrade your references and re-test