Since its first release, the most critical element in Entity Framework
has been the
ObjectContext. It is this
class that allows us to interact with a database using a conceptual model.
The context lets us express and execute queries, track changes to objects
and persist those changes back to the database. The
ObjectContext class interacts with other important
Entity Framework classes such as the
ObjectSet, which enables set operations on our
entities in memory, and
which is the brains behind executing queries. All of these classes are
replete with features and functionality—some of it complex and much of it
only necessary for special cases. After two iterations of Entity Framework
(in .NET 3.5 SP1 and .NET 4) it was clear that developers were most commonly
using a subset of the features, and unfortunately, some of the tasks we
needed to do most frequently were difficult to discover and code.
Recognizing this, the Entity Framework team set out to make it easier
for developers to access the most frequently used patterns for working with
objects within Entity Framework. Their solution was a new set of classes
that encapsulate this subset of
ObjectContext features. These new classes use the
ObjectContext behind the scenes, but
developers can work with them without having to tangle with the
ObjectContext unless they need to specifically use
some of the more advanced features. The new set of classes was originally
released as part of Entity Framework 4.1 (EF 4.1).
The prominent classes in this simplified API surface are the
DbQuery. This entire package of new logic is
referred to as the DbContext API. The new API contains more than just the
DbContext class, but it is the
DbContext that orchestrates all of the new
The DbContext API is available in the EntityFramework.dll assembly, which also contains the logic that drives Entity Framework Code First. This assembly is separate from .NET and is even deployed separately as the EntityFramework NuGet package. A major portion of the Entity Framework is part of the .NET Framework (primarily System.Data.Entity.dll). The components that are included in .NET are considered the “core components” of Entity Framework. The DbContext API is completely dependent on these core components of Entity Framework. The Entity Framework team has indicated that they are working to move more of these core components out of .NET and into the EntityFramework.dll assembly. This will allow them to deliver more features between releases of the .NET Framework.
In Table 1-1, you can see a list of the high-level features and classes in the DbContext API, how they relate to the API surface from Entity Framework 4 (EF4), their general purpose, and their benefits.
Table 1-1. Overview of DbContext API features
DbContext API feature
Relevant EF4 feature/class
Benefit of DbContext API
Represent a session with the database. Provide query, change tracking and save capabilities.
Exposes and simplifies most commonly used features of ObjectContext.
Provide set operations for entity types, such as Add, Attach and Remove. Inherits from DbQuery to expose query capabilities.
Exposes and simplifies most commonly used features of ObjectSet.
Provide querying capabilities.
The query functionality of DbQuery is exposed on DbSet, so you don’t have to interact with DbQuery directly.
Change Tracker API
Get access to change tracking information and operations (e.g., original values, current values) managed by the context.
Simpler and more intuitive API surface.
Provide automatic validation of data at the data layer. This API takes advantage of validation features already existing in .NET 4.
New to DbContext API.
Code First Model Building
Reads classes and code-based configurations to build in-memory model, metadata and relevant database.
New to DbContext API.
The DbContext API is not released as part of the .NET Framework. In order to be more flexible (and frequent) with releasing new features to Code First and the DbContext API, the Entity Framework team distributes EntityFramework.dll through Microsoft’s NuGet distribution feature. NuGet allows you to add references to your .NET projects by pulling the relevant DLLs directly into your project from the Web. A Visual Studio extension called the Library Package Manager provides an easy way to pull the appropriate assembly from the Web into your projects. Figure 1-1 displays a screenshot of the Library Package Manager being used to download and add the EntityFramework NuGet package into a project.