juniorEntity Framework
What is a DbContext in EF Core?
Updated Apr 28, 2026
Short answer
DbContext is the primary class responsible for interacting with the database, representing a session with the database.
Deep explanation
It manages the database connection, configures model mappings via OnModelCreating, acts as an Identity Map to track changes, and provides DbSet properties to query and save instances of entities.
Real-world example
Injecting StoreContext into an ASP.NET Core controller to fetch a specific product from the database.
Common mistakes
- Failing to register DbContext as a Scoped service in dependency injection, leading to concurrency issues.
Follow-up questions
- What is DbContextOptions?