dotnet / dotnet/EntityFramework.Docs

Concerning DbContext disposal

Open
#4,706 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

### Type of issue

Missing information

### Description

Hi, I am new to EF Core and I have a comment about this part about DbContext (of the page with URL provides below)

" ...
public void DoSomething()
{
using (var context = _contextFactory.CreateDbContext())
{
// ...
}
}

Notice that the DbContext instances created in this way are not managed by the application's service provider and therefore must be disposed by the application.
..."

My interpretation is that the using statement above will take care for correctly disposing the created context object. However, it makes me think that I should implement the Dispose pattern. Therefore I suggest to improve this part to avoid possible confusion for less experienced developers. If my assumption is correct, I would rather say something along this line (?):

"...
Notice that the using statement above also disposes the created instance. This is necessary since DbContext instances created this way are not managed by the application's service provider and therefore must be disposed by the application.
..."

Best regards,

### Page URL

https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/

### Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/dbcontext-configuration/index.md

### Document Version Independent Id

b0a3d5ad-b62c-1ff4-386a-f11e350eea0c

### Article author

@ajcvickers

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.