DbContext and DbSet
DbContext is the primary class you use to interact with a database in EF Core; it represents a session with the database and combines the Unit of Work and Repository patterns. DbSet<TEntity> properties on your DbContext represent collections of entities that map to tables you can query and persist.
Cricket analogy: A DbContext is like a match day squad sheet for a team such as Mumbai Indians — one session object holding separate lists (DbSets) for batsmen, bowlers, and support staff, all tracked together for that game.
Defining a DbContext
You create a DbContext by subclassing DbContext, exposing DbSet<T> properties for each entity type, and passing configuration through its constructor (or overriding OnConfiguring) so the context knows which provider and connection to use.
Cricket analogy: Like a franchise creating its own team roster class extending a generic 'Squad' template with named slots for Batsmen and Bowlers, you subclass DbContext and add named DbSet<T> properties for each entity.
DbSet and Change Tracking
Each DbSet<TEntity> is backed by EF Core's change tracker, which monitors entity state (Added, Modified, Deleted, Unchanged) so that when SaveChanges() runs, EF Core knows exactly which INSERT, UPDATE, or DELETE statements to generate.
Cricket analogy: Like a scorer marking each player's status for the innings — 'Not Out', 'Retired Hurt', 'New Batsman In' — EF Core's change tracker tags each entity as Unchanged, Modified, or Added so SaveChanges knows what to do.
Configuring Entities with the Fluent API
Beyond DbSet declarations, you can override OnModelCreating to configure keys, relationships, and constraints via the fluent API on ModelBuilder, giving fine-grained control beyond what data annotations alone can express.
Cricket analogy: Like a coach fine-tuning a generic training plan with specific instructions for a player such as Jasprit Bumrah's bowling workload, the fluent API in OnModelCreating lets you fine-tune entity configuration beyond default conventions.
public class BookStoreContext : DbContext
{
public BookStoreContext(DbContextOptions<BookStoreContext> options) : base(options) { }
public DbSet<Book> Books => Set<Book>();
public DbSet<Author> Authors => Set<Author>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>()
.HasOne(b => b.Author)
.WithMany(a => a.Books)
.HasForeignKey(b => b.AuthorId);
modelBuilder.Entity<Book>()
.Property(b => b.Title)
.IsRequired()
.HasMaxLength(200);
}
}DbContext instances are lightweight and cheap to create but are NOT thread-safe — never share one DbContext instance across concurrent operations or parallel tasks; request a new scoped instance instead.
- DbContext represents a session with the database and tracks entity state.
- DbSet<TEntity> properties expose queryable, persistable collections mapped to tables.
- You subclass DbContext and typically inject DbContextOptions via its constructor.
- The change tracker marks entities as Added, Modified, Deleted, or Unchanged.
- OnModelCreating and the fluent API (ModelBuilder) configure keys, relationships, and constraints.
- DbContext instances are not thread-safe and should not be shared across concurrent operations.
Practice what you learned
1. What does a DbSet<TEntity> property on a DbContext represent?
2. Which method do you override to configure entities using the fluent API?
3. Which entity states does EF Core's change tracker use?
4. Why is it unsafe to share one DbContext instance across concurrent requests?
5. How do you typically configure a relationship like Book -> Author beyond default conventions?
Was this page helpful?
You May Also Like
What Is Entity Framework Core?
An introduction to Entity Framework Core, Microsoft's cross-platform ORM for .NET, and why teams use it instead of raw ADO.NET.
Installing and Configuring EF Core
Step-by-step guide to installing EF Core NuGet packages, configuring connection strings, and registering DbContext with dependency injection.
Your First Query
Writing and executing your first LINQ query against EF Core, including deferred execution, projections, async queries, and the N+1 pitfall.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics