DbContext API: Overview

Description Article
Summary

The DbContext API is a solution that wraps and encapsulates all the complexity of the Entity Framework components, working as bridge between the Domain Model (Entity classes) and the Database.

DbContext Architecture

Description

The DbContext class is the primary class and is responsible for interacting with data as an object : it is a wrapper around the ObjectContext class and can be used with all the development models: Code First, Model First and Database First.

DbContext API Feature/Class EF Feature/Class Purpose Benefit of DbContext API
DbContext ObjectContext Primary class.
Represents a session with the database. Provide query, change tracking, persistency and object materialization capabilities
Exposes and simplifies the most important and commonly used features of the ObjectContext class
DbSet ObjectSet Contains an entity set (DbSet<tEntity>) for all the entities which is mapped to database tables. Provide set operations like Add, Remove, Attach, etc Exposes and simplifies the most important and commonly used features of the ObjectSet class
DbQuery ObjectQuery Provide querying capabilities. DbContext converts Linq To Entities queries to SQL query and send it to the database The query functionality is exposed on DbSet and therefore there is no need to interact directly with DbQuery and ObjectQuery
Change Tracker API ObjectContext.ObjectStateManager Get access to change tracking information and operations managed by the data context Simpler and more intuitive API
Validation API n/a Provide automatic validation of data at the data layer
Code First Model Building n/a Reads classes and code-based configurations to build in-memory model, metadata and relevant database

 

Assemblies

DbContext was first released as part of the Entity Framework 4.1 (EF4). Therefore it was initially distributed through:

  1. The .NET Framework – core components on System.Data.Entity.dll assembly; and
  2. The EntityFramework NuGet Package which includes the EntityFramework.dll assembly.

In the current version EF6 the DbContext API is available in the Entity.Framework.dll assembly and is independent of the .NET Framework.

References

[1] Refer to this to upgrade to EF6.
[2] Refer to this article to install EntityFramework NuGet Package.
[3] Refer to this alternative article to install EntityFramework NuGet Package.