liveBook · Manning

Dbcontext

As explained in 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.

A DbContext is the database-session abstraction between an ASP.NET Core application and its database. It lets C# code work with entities and perform CRUD operations. In a board-game API, an ApplicationDbContext provides access to board games, domains, mechanics, and their relationships. section 4.3.3

Listing 4.1 ApplicationDbContext.cs file
using Microsoft.EntityFrameworkCore;

namespace MyBGList.Models
{
   public class ApplicationDbContext : DbContext
   {
       public ApplicationDbContext(
           DbContextOptions<ApplicationDbContext> options)
           : base(options)
       {
       }

       protected override void OnModelCreating(ModelBuilder modelBuilder)
       {
           base.OnModelCreating(modelBuilder);

           // TODO: custom code here
       }
   }
}

Start by deriving ApplicationDbContext from DbContext. Its constructor passes DbContextOptions<ApplicationDbContext> to the base class; OnModelCreating is the place for model-wide configuration. section 4.3.3

EF Core can map a model through conventions, data annotations, or the ModelBuilder Fluent API. Use DbSet properties to expose entity sets, and use OnModelCreating when a mapping needs explicit rules, such as a join entity’s composite key. Fluent API rules take precedence over data annotations, which take precedence over conventions. section 4.3.3

To put the context to work in the API:

  • Register ApplicationDbContext in Program.cs with AddDbContext, configuring UseSqlServer with the DefaultConnection connection string. section 4.3.4
  • Inject ApplicationDbContext into a controller constructor and keep the reference available to its action methods. section 5.2
  • Query a DbSet in an action method. Calling ToArray() on the board-game query executes a database SELECT and returns an array of BoardGame entities instead of dummy data. section 5.2
builder.Services.AddDbContext<ApplicationDbContext>(options =>
   options.UseSqlServer(
       builder.Configuration.GetConnectionString("DefaultConnection"))
   );

This registration connects the context to SQL Server using a named connection string supplied through configuration. section 4.3.4

Treat a context instance as one unit of work: a set of database operations for a business transaction. In a typical web API, that work fits within one HTTP request and response. The controller can obtain the context through dependency injection rather than constructing its own database session. section 4.3.4 section 5.2

How Building Web APIs with ASP.NET Core covers it

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 it assumes
Assumes an ASP.NET Core web API with Program.cs and a controller, while defining DbContext’s database-session role and the unit of work.
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.
How it connects
Connects DbContext to dependency injection, unit of work, DbSet, Fluent API, data annotations, conventions, and IQueryable.

Also explained in

Compare how 4 books explain dbcontext →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.