Introduction
ObjectContext and DbContext have the capability to query and work with data as objects. Additionally, Dbcontext can be represented as a combination of the Unit of Work and Repository patterns. Conceptually DbContext is the same as ObjectContext.
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 built-in 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 things
- 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 an 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
| ObjectContext | DbContext |
| ObjectContext class is part of the core Entity Framework API, which 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. |
| ObjectContext supports Compiled Queries. | DbContext does not support Compiled Queries. |
| ObjectContext supports the self-tracking of Entities | DbContext does not support the self-tracking of Entities. |
| ObjectContext is only useful in Model First and Database First approaches | DbContext is useful in the Model First, Database First approach as well as Code First approach. |
| ObjectContext can be used by Entity Framework 4.0 and below. | DBContext can be used by Entity Framework 4.1 and above. |
| 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. |

Pasang TamangPosted Dec 23, 2022, 5:56 AM
Nice article with good explanation
Uday DodiyaPosted Aug 13, 2022, 9:00 AM
Excellent work
Pawan VermaPosted Sep 25, 2019, 2:09 AM
Dbcontext is lightweight version of Object context. How can you prove it?
Dipak ShindePosted Jun 10, 2017, 5:22 AM
Which one better to use ObjectContext Or DbCOntext
Bhavik PatelPosted May 17, 2016, 12:27 AM
good one
Mahesh AllePosted Mar 10, 2016, 10:36 AM
Excellent work.... Thanks....
Pramod LawatePosted Oct 21, 2015, 4:38 AM
good explanation ..
Sudheer KumarPosted Nov 3, 2014, 3:12 AM
Good article
Sai SherlekarPosted Jun 20, 2014, 7:30 AM
good
Hendrawan HaryantoPosted Oct 21, 2013, 12:19 AM
wow great explanationss..