ObjectContext
ObjectContext is a class that manages all the database operations, like database connection, and manages various entities of the Entity Model. We can say that ObjectContext is the primary class for accessing or working together with entities that are defined in the conceptual model.
ObjectContext is responsible for:
Database connection
It provides builtin Add, Update and Delete functions
Object Set of every entity
Provide State of pending changes
It holds the changes done in entities
ObjectContext also encapsulates a few of things; they are:
Connection to the Data Store or Database
Metadata in the Entity Data Model (EDM)
ObjectStateManager to track changes to the objects
DbContext
DbContext is conceptually similar to ObjectContext. DbContext is nothing but a ObjectContext wrapper, we can say it is a lightweight alternative to the ObjectContext. DbContext can be used for DataBase first, code first and model first development. DbContext mainly contains a set of APIs that are very easy to use. The API is exposed by ObjectContext. These APIs also allow us to use a Code First approach that ObjectContext does not allow.
ObjectContext VS DbContext
1. ObjectContext class is part of the core Entity Framework API, that allows us to perform queries, change and track updates of the Database by using strongly typed entity classes.
The DbContext class can be described as a wrapper of ObjectContext. It exposes the most commonly used features of ObjectContext.
2. ObjectContext supports Compiled Queries.
DbContext does not support Compiled Queries.
3. ObjectContext supports self-tracking of Entities
DbContext does not support self-tracking of Entities.
4. ObjectContext is only useful in Model First and Database First approaches
DbContext is useful in Model First, Database First approach as well as Code First approach.
5. ObjectContext can be used by Entity Framework 4.0 and below.
DBContext can be used by Entity Framework 4.1 and above.
6. The ObjectContext class is not thread-safe.
Any public static (C#) or Shared (Visual Basic) members of DbContext are thread-safe. Any instance members are not guaranteed to be thread safe.