dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.56k stars 1.94k forks source link

Concerning DbContext disposal #4706

Open PeterKMX opened 1 month ago

PeterKMX commented 1 month ago

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

roji commented 1 month ago

@PeterKMX the EF docs do assume certain basic knowledge of C# and .NET, such as how IDisposable works and the using statement; having to explain basic C# concepts everywhere in our docs would make them very unwieldy and hostile to people who already know C#.