4

I'm using the recommended approach to create DbContext instance through dependency injection.

In Startup.cs -

services.AddDbContext<DashboardContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DashboardConnection")));

and in the Controller -

private readonly DashboardContext db;
public AccountController(DashboardContext context)
{
    db = context;
}

What I want to know is when this instance gets disposed.

Previously we would always use the using statement which would dispose on close of braces -

using (DashboardContext db = new DashboardContext())
{
    // Query
}
3
  • 4
    AddDbContext<>() registers the DBContext as a service with ServiceLifetime.Scoped which means that your DbContext is created per web request. It is disposed when request is completed. Commented Jan 27, 2021 at 5:19
  • 1
    Check the DBContext implementation at here Commented Jan 27, 2021 at 5:32
  • The DbContext will be disposed as soon as the request ends. This gives you "transaction-per-request" out of the box - if you don't want to persist changes, don't call SaveChanges before exiting the action. Commented Jan 27, 2021 at 7:26

1 Answer 1

7

With the AddDbContext method, a DbContext will be created with Scoped lifetime by default; which means, it's lifetime is scoped within the current request, and it will get disposed as soon as the current request completes.

But you can override the default by passing a value for the contextLifetime parameter, like -

services.AddDbContext<DashboardContext>(options => 
    options.UseSqlServer(
        Configuration.GetConnectionString("DashboardConnection")),
        ServiceLifetime.Transient);

For further detail check - AddDbContext

EDIT - (in reply to @Dale's comment) :
Considering the overall architecture of the ASP.NET Core MVC, and how we tend to use the framework, I'd say (personal opinion) that in general for most applications its better to stick to the default Scoped lifetime.
In the answer, I just wanted to make it clear that the option for manual config is there for you. Of course, there might be use cases or scenarios (depending on how you design your own application) where the manual config has its use.

Sign up to request clarification or add additional context in comments.

2 Comments

Thats super helpfull, Im a little unsure on in a normal ASP.NET Core 5 MVC application if Scoped or Transient is better I read your link and while it explains them, its not clear what would work best.
@DaleFraser Let's say a controller has a dependency on IBob and ICathy. Cathy (the ICathy) also has a dependency on IBob. Do you want the controller and Cathy to share the same IBob? If so - IBob should be Scoped. If not - IBob should be Transient. Disposal wise - Transient and Scoped will act the same, they'll Dispose when the web request ends. I'd argue for most scenarios Transient is the safer default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.