Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document best practices for stateful context pooling #3706

Closed
GregJF opened this issue Jan 30, 2022 · 1 comment · Fixed by #3709
Closed

Document best practices for stateful context pooling #3706

GregJF opened this issue Jan 30, 2022 · 1 comment · Fixed by #3709

Comments

@GregJF
Copy link

GregJF commented Jan 30, 2022

[Enter feedback here]
You said

Context pooling works by reusing the same context instance across requests. This means that it's effectively registered as a Singleton in terms of the instance itself so that it's able to persist.

registering a dbContext as Singleton is not advisable as all users will share the same instance for the life of the application
The better way is to register dbContext as Scoped (in .net core) or webRequest (in most IoC products)

Regards
GregJF


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@roji
Copy link
Member

roji commented Jan 31, 2022

@GregJF the documentation describes a limitation in the way DbContext pooling works; since context instances are pooled across requests, the lifetime of each context instance is effectively like a singleton-registered service (although the context instances themselves aren't directly registered in DI at all). This is a fundamental characteristic of how pooling works.

However, we should probably add some guidance on how to work with pooled contexts that need to vary state across requests (i.e. scoped IDbContextFactory, DbConnection interception for connection strings). dotnet/efcore#14625 has some discussion around these. /cc @ajcvickers

@roji roji changed the title Questionable advice Document best practices for stateful context pooling Jan 31, 2022
@roji roji self-assigned this Jan 31, 2022
@roji roji added this to the 7.0.0 milestone Jan 31, 2022
roji added a commit to roji/EntityFramework.Docs that referenced this issue Jan 31, 2022
And rearrange the perf docs a little

Closes dotnet#3706
roji added a commit to roji/EntityFramework.Docs that referenced this issue Jan 31, 2022
And rearrange the perf docs a little

Closes dotnet#3706
roji added a commit to roji/EntityFramework.Docs that referenced this issue Jan 31, 2022
And rearrange the perf docs a little

Closes dotnet#3706
roji added a commit to roji/EntityFramework.Docs that referenced this issue Jan 31, 2022
And rearrange the perf docs a little

Closes dotnet#3706
roji added a commit to roji/EntityFramework.Docs that referenced this issue Jan 31, 2022
And rearrange the perf docs a little

Closes dotnet#3706
roji added a commit to roji/EntityFramework.Docs that referenced this issue Jan 31, 2022
And rearrange the perf docs a little

Closes dotnet#3706
@roji roji closed this as completed in #3709 Mar 2, 2022
roji added a commit that referenced this issue Mar 2, 2022
And rearrange the perf docs a little

Closes #3706
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants