Sitemap
CodeToDeploy

Tech Insights | Careers | Jobs

Why DbContext Is Scoped and Not Singleton

3 min readJun 15, 2026

--

Understanding dependency injection lifetime, thread safety, and the unit-of-work pattern in Entity Framework

Click here to read this story for free

You’ve probably seen this registration in ASP.NET Core:

builder.Services.AddDbContext<AppDbContext>();

By default, AddDbContext() registers DbContext as Scoped. But why not make it a Singleton and reuse same instance throughout the application's lifetime?

Well, well, this isn’t just arbitrary — it’s essential for correctness, performance, and thread safety.

Understanding DbContext

DbContext represents a session with the database. It tracks entity changes, manages database operations, and maintains an internal state.

Think of it as a unit of work for a single request.

Press enter or click to view image in full size

💥 Master Any Skills in 3 Months
📚 Up to 50% OFF Premium Courses
⏰ Limited-Time Offer
👉 Enroll Now & Start Learning

Press enter or click to view image in full size

Why Scoped Is the Right Choice

--

--

I turn real engineering messes into easy-to-understand guides you can actually use. Subscribe for more