-
Notifications
You must be signed in to change notification settings - Fork 273
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
Need way to disable disposing of DbContext when root DbContextScope is disposed #51
Comments
Can you just not dispose the DbContextScope? |
Jaap-van-Hengstum, please, share your decision, if your figured it out. I'm really on the same page with about this question. |
In short words, the answer is: register your DbContext as Transient lifetime. And ... the long story: So, how DbContextScope does his job (as long, as I debugged sources):
The solution is to allow your serviceProvider to recreate DbContext for each request during Scoped lifetime of your Web request: |
The way this library works now is that the
DbContext
instances are disposed when the rootDbContextScope
is disposed.I'm using this library in an OData Web Api project. The behaviour of disposing
DbContext
instances is causing problems with the OData pipeline because OData expects theDbContext
to be open until very late in the Web Api pipeline. The only way of keeping theDbContext
open is to create theDbContextScope
very early in the process in the Owin middleware, but I didn't want to do that because I want to have a readonly or readwrite scope depending on the requested controller action, and this is not yet known at the Owin stage.So I had to resort to trickery to keep the
DbContext
from being disposed right away, which was to override theDispose(bool disposing)
method of theDbContext
and add anotherManualDispose()
method to dispose of theDbContext
at a later time.It would be nice if it were possible to create a
DbContextScope
with the option to disable auto disposing of theDbContext
instances.Or does someone have another suggestion to tackle this issue?
The text was updated successfully, but these errors were encountered: