liveBook · Manning

Dbcontext

Also called database context

DbContext is the Entity Framework Core class that coordinates database access, entity querying and change tracking, and persistence for an application’s data model.

How 4 Manning books explain it

ASP.NET Core in Action, 3e

DbContext is the Entity Framework Core base class that coordinates database connections, entity tracking, querying, and persistence for a data model.

How it is used
Uses a custom context to configure EF Core and access the database from an application service.
How deep it goes
Explains model discovery, registration, change tracking, and scoped lifetime; uses conventions while deferring detailed customization.
Examples
Uses a recipe-and-ingredient model, a recipe update, and a background service storing exchange rates.
What you can do after
Apply it using EF Core to define and register a context, update tracked entities, and manage scoped access in background services.

Read how ASP.NET Core in Action, 3e explains dbcontext →

Building Web APIs with ASP.NET Core

DbContext is the Entity Framework Core class that coordinates database queries, entity tracking, and persistence for an application session.

How it is used
Provides the database session used by an ASP.NET Core controller to query board games instead of returning dummy data.
How deep it goes
Walks through subclass creation, model configuration, service registration, controller injection, and query execution; it does not detail EF Core internals.
Examples
Uses an ApplicationDbContext, SQL Server registration, and a board-game controller GET action that replaces generated data with a database query.
What you can do after
Apply it via Entity Framework Core to register a context, inject it into a controller, and retrieve board-game entities.

Read how Building Web APIs with ASP.NET Core explains dbcontext →

.NET in Action, Second Edition

DbContext is the Entity Framework Core class that coordinates entity operations, querying, change tracking, and persistence for a data model.

How it is used
Coordinates entity persistence and queries in a catalog application and its ASP.NET Core API.
How deep it goes
Builds a context, demonstrates its lifetime and registration, and revises provider configuration; stops short of change-tracking internals.
Examples
Uses a book catalog that seeds books, fetches books with ratings, and serves them through controller actions.
What you can do after
Build it from scratch as an EF Core context, then operate it in queries and configure its lifetime in an API.

Read how .NET in Action, Second Edition explains dbcontext →

Pro ASP.NET Core 7, Tenth Edition

DbContext is the Entity Framework Core base class that provides database access, entity tracking, and persistence services for a data context.

How it is used
Provides database access to ASP.NET Core request handlers through an Entity Framework Core context registered as a service.
How deep it goes
Builds and configures derived contexts, then demonstrates querying and saving; does not detail DbContext internals.
Examples
Uses product and calculation contexts, a calculation endpoint, SQL logs, and a supplier query that triggers an object-cycle exception.
What you can do after
Build it from scratch: define a derived context, register its SQL Server connection, and use it to query and save entities.

Read how Pro ASP.NET Core 7, Tenth Edition explains dbcontext →

Also covered in

What to know first

Related concepts

Questions about dbcontext

What is dbcontext?

DbContext is the Entity Framework Core class that coordinates database access, entity querying and change tracking, and persistence for an application’s data model.

Which Manning books explain dbcontext?

ASP.NET Core in Action, 3e, Building Web APIs with ASP.NET Core, .NET in Action, Second Edition and Pro ASP.NET Core 7, Tenth Edition.

What should I know before learning dbcontext?

Manning books that teach dbcontext usually introduce foreign key, database table and array first.

What kinds of dbcontext are there?

Kinds of dbcontext include IdentityDbContext.

What is dbcontext also called?

Dbcontext is also called database context.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.