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.

DbSet<T> is an Entity Framework Core container for entities of one type. It makes those entities available through a DbContext for querying and create, read, update, and delete operations. As a useful analogy, if an entity represents a table record, its DbSet represents the table. section 4.3.3

A DbContext typically exposes a public DbSet property for each entity type. For a board-game API, ApplicationDbContext can expose BoardGames, Domains, Mechanics, and the junction entities that connect games to domains and mechanics. Each property uses Set<T>() to provide the corresponding set. 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 exposes five entity sets, including sets for the two junction entities. section 4.3.3

DbSet<T> implements IQueryable<T>, so it can be the starting point for a LINQ query. Operators such as Where and OrderBy build a query representation; EF Core converts it to a database query when the IQueryable is executed. section 5.1.3

var query = _context.BoardGames
   .Where(b => b.Year == 2020)
   .OrderBy(b => b.Name);

This query selects games from 2020 and orders them by name. It can be executed later to retrieve the results. section 5.1.3

Importing board games

A CSV import reads existing games, domains, and mechanics into dictionaries, then skips records with missing required fields or duplicate game IDs. It adds new games to the BoardGames set, reuses or adds domains and mechanics, and creates junction entities for their relationships. EF Core tracks the additions in memory and assigns temporary keys; SaveChangesAsync persists the changes. section 5.3.5

When extending the model with categories, add sets for both the Category entity and its BoardGames_Categories junction entity. The relationship also needs navigation properties, foreign keys, and cascading rules. section 4.4.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines the set and walks through properties, queries, additions, change tracking, and deferred query execution.
Examples
Uses a CSV board-game import, a filtered and ordered game query, and a category junction-entity exercise.
What it assumes
Assumes familiarity with polymorphism while defining DbSet and its relationship to IQueryable<T>.
What you can do after
Apply it via Entity Framework Core to expose entity sets, compose queries, and add entities before saving changes.
How it connects
Connects DbSet<T> to DbContext, IQueryable<T>, LINQ, change tracking, and many-to-many relationships.

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.