question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mannokcommented, Oct 28, 2022

The new DisposeDbContext setting was added on version 20.0.3, please upgrade your references and re-test

@thepirat000 Thank you so much for your prompt action. The most efficient maintainer I have ever seen.

0reactions
thepirat000commented, Oct 28, 2022

The new DisposeDbContext setting was added on version 20.0.3, please upgrade your references and re-test

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found