ApplicationDbContext is the Entity Framework Core context for the board-game API. It connects the application’s C# data model to its database, giving controllers a way to work with persistent records rather than manually generated data. section 4.5 section 5.2
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 with a class derived from DbContext. Its constructor passes DbContextOptions<ApplicationDbContext> to the base class; OnModelCreating is the place for model configuration beyond the default conventions. section 4.3.3
Build the context around the entities and relationships the API needs:
- Expose BoardGames, Domains, Mechanics, and their junction entities as DbSet properties. Controllers can then access those sets through the context. section 4.3.3
- Override OnModelCreating to configure each junction entity’s composite primary key with HasKey. Two [Key] attributes on an entity are not enough for this configuration. section 4.3.3
- Configure navigation properties, foreign keys, required relationships, and cascade-delete behavior with the Fluent API. section 4.3.3
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<BoardGames_Domains>()
.HasKey(i => new { i.BoardGameId, i.DomainId });
modelBuilder.Entity<BoardGames_Mechanics>()
.HasKey(i => new { i.BoardGameId, i.MechanicId });
}
The two HasKey calls show how each board-game relationship gets a composite key made from the IDs of its related entities. section 4.3.3
Register ApplicationDbContext in Program.cs with AddDbContext, selecting SQL Server and the DefaultConnection connection string. For development, the connection string can be kept in Visual Studio Secret Manager. EF Core migrations can then create the database structure from the model. section 4.3.4 section 4.5
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection"))
);
This registration connects the context to SQL Server using the configured DefaultConnection value. section 4.3.4
An injected context is typically used for one unit of work within an HTTP request/response lifecycle. A controller receives it through its constructor and keeps a reference for its action methods. section 4.3.4 section 5.2
Read and seed board games
To replace a controller’s dummy data, read _context.BoardGames and execute the query to retrieve BoardGame entities. For CSV seeding, add board games, domains, mechanics, and junction entities to their sets; these additions are tracked in memory until SaveChangesAsync persists them. The seed response can report entity counts and skipped rows as a check on the result. section 5.2 section 5.3.5
When adding another relationship, update both the entities and the context: the publisher exercise calls for a one-to-many relationship and DbSet<Publisher>; the category exercise calls for a many-to-many junction entity and its DbSets. section 4.4.2 section 4.4.3
