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

Explained Blazor Server specifics for configuring-dbcontext.md #2201

Merged
merged 1 commit into from
Mar 20, 2020

Conversation

akovac35
Copy link
Contributor

For Blazor Server hosting model, one logical request is used for maintaining the Blazor user circuit, and thus only one scoped DbContext instance is available per user circuit if the default injection scope is used.

…text.md

For Blazor Server hosting model, one logical request is used for maintaining the Blazor user circuit, and thus only one scoped DbContext instance is available per user circuit if the default injection scope is used.
@roji
Copy link
Member

roji commented Mar 20, 2020

Thanks for this - ideally guidance would be provided for how to deal with this, including samples (e.g. register the context as transient?). Also, we'd need to document the situation for client-side Blazor, not just server-side.

Finally, this guidance probably belongs more on the ASP.NET side, with links from EF Core.

@smitpatel
Copy link
Member

@akovac35
Copy link
Contributor Author

akovac35 commented Mar 20, 2020

Thanks for this - ideally guidance would be provided for how to deal with this, including samples (e.g. register the context as transient?). Also, we'd need to document the situation for client-side Blazor, not just server-side.

Finally, this guidance probably belongs more on the ASP.NET side, with links from EF Core.

Guidance is already provided just a few lines down from my update:


However any code that explicitly executes multiple threads in parallel should ensure that DbContext instances aren't ever accessed concurrently.

Using dependency injection, this can be achieved by either registering the context as scoped and creating scopes (using IServiceScopeFactory) for each thread, or by registering the DbContext as transient (using the overload of AddDbContext which takes a ServiceLifetime parameter).


It makes sense to have Blazor specific detail mentioned on this particular page. Perhaps my one-line suggestion could be rewritten to be more generic (other changes should remain for consistency):

A specific use case where additional consideration is required is scoped dependency injection in Blazor applications: ASP.NET Core Blazor dependency injection

@ajcvickers
Copy link
Member

@akovac35 Thanks for submitting this. We're going to merge it because it improves what we currently have. However, #2025 covers having much better guidance here.

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

Successfully merging this pull request may close these issues.

4 participants