Skip to main content

What is the difference between DbContext and DbSet?

Mid ASP.NET Web API
Quick Answer DbContext is the unit of work and gateway to the database. It manages the connection and tracks entity changes. DbSet represents a table - use it to query (LINQ) and modify (Add, Remove) entities. One DbContext instance per request (Scoped). Multiple DbSet properties (one per entity type) in a DbContext. SaveChangesAsync() persists all tracked changes to the DB in one transaction.

Answer

DbContext manages the database connection, querying, saving, and tracking of entities.

DbSet<T> represents a table and allows querying and CRUD operations for that entity type.

S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice