AddDbContext<T> is an ASP.NET Core dependency-injection extension method that registers an Entity Framework Core database context type and configures its provider and options.
AddDbContext<T> registers an Entity Framework Core DbContext with ASP.NET Core dependency injection. Its configuration selects a database provider and supplies the connection string used at runtime. section 12.2.3
Listing 12.4 Registering a DbContext with the DI container
Register AppDbContext in Program.cs by reading DefaultConnection from configuration and passing it to UseSqlite. section 12.2.3
A typical registration:
Load the environment-specific connection string from configuration. section 12.2.3
Call AddDbContext<AppDbContext> with an options configuration function. section 12.2.3
Select the provider with UseSqlite or the appropriate Use* method for another database. section 12.2.3
Use the resulting scoped DbContext through dependency injection; migrations and database updates remain separate operations. section 12.2.3section 12.6
How ASP.NET Core in Action, 3e covers it
How deep it goes
Explains the options configuration function, provider selection, connection loading, and scoped lifetime without detailing provider internals.
Examples
Uses AppDbContext, DefaultConnection, Program.cs, UseSqlite, and the Microsoft.EntityFrameworkCore.SQLite package.
What you can do after
Operate/configure an ASP.NET Core application so injected DbContext instances can interact with EF Core and its database.
How it connects
Connects DbContext, dependency injection, EF Core providers, connection strings, configuration, and migrations.
.NET in Action, Second EditionAddDbContext is an Entity Framework Core service-registration method that registers a DbContext type with a dependency injection container.
Entity Framework Coreuses · Entity Framework Core is a cross-platform object-relational mapping framework for .NET that enables applications to query and persist data through .NET objects and APIs.
dependency injectionconfigures · Dependency injection is a software design technique in which a component receives its dependencies from outside rather than creating or locating them internally.
dbcontextconfigures · DbContext is the Entity Framework Core class that coordinates database access, entity querying and change tracking, and persistence for an application’s data model.
dependency injection containerconfigures · A dependency injection container is software that constructs and resolves objects by supplying their configured dependencies, often managing their lifetimes.
This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.