liveBook · Manning

DbSet<T>

As explained in Building Web APIs with ASP.NET Core

DbSet<T> is an Entity Framework Core type representing a set of entities of a specified type and providing query and database operations for that set.

A DbSet<T> is the Entity Framework Core entry point for a set of entities of type T. If a BoardGame object represents one database record, a DbSet<BoardGame> represents the set of board-game records. It lets an application query that set and perform CRUD operations on it. section 4.3.3

Expose entity sets as public properties on a DbContext:

  • Add a property such as `public DbSet<BoardGame> BoardGames => Set<BoardGame>();` to ApplicationDbContext. The property gives application code a named way to reach board games. section 4.3.3
  • Add properties for the other entity types the context must expose, including Domain, Mechanic, and the two junction entities linking them to board games. section 4.3.3
using Microsoft.EntityFrameworkCore;
 
namespace MyBGList.Models
{
    public class ApplicationDbContext : DbContext
    {
 
        // ... existing code
 
public DbSet<BoardGame> BoardGames => Set<BoardGame>();
public DbSet<Domain> Domains => Set<Domain>();
public DbSet<Mechanic> Mechanics => Set<Mechanic>();
public DbSet<BoardGames_Domains> BoardGames_Domains
=> Set<BoardGames_Domains>();
public DbSet<BoardGames_Mechanics> BoardGames_Mechanics => Set<BoardGames_Mechanics>();
    }
}

The context listing shows the five public DbSet properties together. Each property's generic type identifies its entity type; `Set<T>()` supplies the corresponding set. section 4.3.3

Query board games

`var query = _context.BoardGames.Where(b => b.Year == 2020);` starts with a DbSet<BoardGame> and adds a LINQ filter. DbSet<T> implements IQueryable<T>, so LINQ operators can build a query representation. That query is sent to the database when it is executed, rather than when the variable is assigned. section 5.1.3

Add records from a CSV file

A CSV import checks for existing board games through `_context.BoardGames`, then adds new BoardGame, Domain, and Mechanic entities through their sets. Adding them does not immediately persist them: EF Core tracks the entities and their relationships in memory until `SaveChangesAsync()` saves them. section 5.3.5 section 5.3.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines DbSet<T>, shows property declarations, and distinguishes tracked additions from database persistence without detailing its internals.
Examples
Uses board-game DbSet declarations, a year-filtered query, and a CSV import that adds related entities.
What it assumes
Introduces DbSet from a defined entity and context while assuming familiarity with C# polymorphism for its IQueryable<T> behavior.
What you can do after
Apply it via Entity Framework Core to expose entity sets, query board games, and add imported entities before saving.
How it connects
Connects DbContext, entity types, many-to-many junction entities, IQueryable<T>, LINQ, and change tracking.

Also explained in

Compare how 3 books explain DbSet<T> →

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.