Code Monkey home page Code Monkey logo

Comments (3)

rock-walker avatar rock-walker commented on July 17, 2024 1

In short words, the answer is: register your DbContext as Transient lifetime.

And ... the long story:
In .NET Core 1.1 you register your DbContexts by default in Scoped lifetime, like this:
services.AddDbContext<AttendeeContext>(options => options.UseSqlServer(connectionString)

So, how DbContextScope does his job (as long, as I debugged sources):

  1. in each new scope, (like it usually recommends: using (var dbScope = _factory.Create()) { ... } ) the new collection of DbContexts initialized - not previously initialized DbContexts in the current scope of whole web request.
  2. when the dbScope code block ends, using statement calls Dispose for your DbContextScope and that means exact disposing of all your existing collection of DbContexts.
  3. What does it mean for lifetime of DbContext in scope of lifetime of whole Web request?
    -- it means, that you manually have killed DbContext somewhere in the middle of living your Web request, and DbContext becomes unavailable for next calls inside still alive Web Request.

The solution is to allow your serviceProvider to recreate DbContext for each request during Scoped lifetime of your Web request:
services.AddDbContext<AttendeeContext>(options => options.UseSqlServer(connectionString, ServiceLifeTime.Transient)
And by the way, I also registered IDbContextScopeFactory as Singleton lifetime.

from dbcontextscope.

crush83 avatar crush83 commented on July 17, 2024

Can you just not dispose the DbContextScope?

from dbcontextscope.

rock-walker avatar rock-walker commented on July 17, 2024

Jaap-van-Hengstum, please, share your decision, if your figured it out. I'm really on the same page with about this question.
I play with .NET Core and native DI for my Web API. Tried many approches with DbContextScope , IAmbientContextLocator and their lifetimes. As I understood correctly, DbContextScope should be Transient and AmbientContextLocator - Singleton. But the problem exactly the same - DbContextScope kills DbContext everytime.

from dbcontextscope.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.