DbContext
The EF Core class that represents a session with the database: it tracks entities, translates queries and saves changes as one unit of work.
In more detail
It owns a connection and a change tracker, so it is disposable and registered with a scoped lifetime, one per request. It receives its settings through DbContextOptions from dependency injection.
Related terms
- Entity Framework Core — Microsoft's object-relational mapper for .NET, which lets code query and save database data as C# objects and translates LINQ into SQL.
- DbSet<T> — A property on a DbContext representing one table's entities, which you query with LINQ and add to or remove from.
- unit of work — A pattern that collects the changes made during one business operation and commits them together, or not at all.
- scoped lifetime — A service lifetime in which one instance is shared within a scope, which in ASP.NET Core means one HTTP request, then disposed.
- change tracking — EF Core keeping a snapshot of entities it loaded so that SaveChanges can detect which properties changed and send updates for them.
Taught in
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- The Dependency Injection Container: Scopes and Lifetimes — Modern .NET Core, C# & Enterprise Microservices
- Entity Framework Core: Code First and Migrations — Modern .NET Core, C# & Enterprise Microservices
- Querying, Change Tracking, and Saving Data — Modern .NET Core, C# & Enterprise Microservices
- The Repository Pattern — Modern .NET Core, C# & Enterprise Microservices