scoped lifetime
A service lifetime in which one instance is shared within a scope, which in ASP.NET Core means one HTTP request, then disposed.
In more detail
A DbContext is scoped so that one request's changes form one unit of work. Never inject a scoped service into a singleton.
Related terms
- service lifetime — How long the container keeps and reuses an instance of a service: transient, scoped or singleton.
- DbContext — The EF Core class that represents a session with the database: it tracks entities, translates queries and saves changes as one unit of work.
- unit of work — A pattern that collects the changes made during one business operation and commits them together, or not at all.
- scope validation — A container check that fails when a singleton depends on a scoped service, a bug called a captive dependency.
Taught in
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- The Dependency Injection Container: Scopes and Lifetimes — Modern .NET Core, C# & Enterprise Microservices
- The Options Pattern: Strongly Typed Configuration — Modern .NET Core, C# & Enterprise Microservices
- Entity Framework Core: Code First and Migrations — Modern .NET Core, C# & Enterprise Microservices