-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Make sure that scoping with AddDbContext is a good experience #1112
Comments
Some quick (crazy?) ideas that come to mind, that maybe we can discuss later:
|
I don't think we need to do anything too complex to try and make stuff work here. If you're managing the service provider like this then it's a pretty complex scenario and I think it's fine for you to know you need to create scopes etc. |
I am not sure having some DI setup code in your application's start up means that you want to learn how to create your own DI scopes to control the lifetime of your DbContexts. That feels like a cliff. But I agree we shouldn't do anything too complex. |
Decisions on this:
Clearing for re-triage since I don't think there is anything to actually do here. |
While responding to aspnet/DependencyInjection#352. I had what I think is a new idea that could help improve this experience. Here is a summary
A few details:
@ajcvickers I don't remember if we ever discussed doing something like this. Do you? |
Hmm, I just realized that this is probably the wrong bug as it was originally about the default scoping of |
AddDbContext registers the application's context as a scoped service. This means that within a given scope the same instance is always returned. This is fine when something (e.g. a web framework) is creating and disposing the scope and the application does not also attempt to dispose the context. However, it means that a case like this does not in a not very obvious way:
In this code the second using actually gets the same context instance again, which has already been disposed. The solution right now would be to create a DI scope for each using and dispose that scope, which in turn will dispose each context. For example:
But this is cumbersome and non-obvious. So can we come up with patterns/guidelines/pits of success such that:
The text was updated successfully, but these errors were encountered: