ObjectContext VS DBContext

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.

There are also many methods in common in both ObjectContext and DbContext. For example, ObjectContext has a direct method to execute a query against a database whereas the DbContext.Database class contains the SQLQuery method to obtain the same result.

Example

// using ObjectContext (EF4.0) using (
  Entities context = new Entities()
) {         IEnumerable < EmployeeDetails > empDetails   =  context.ExecuteStoreQuery < EmployeeDetails >     ("exec GetEmployeeData").ToList();
} // using DBContext (
  EF 4.1 
  and above
) using (
  Entities context = new Entities()
) {         IEnumerable < EmployeeDetails > empDetails   =  context.Database.SqlQuery                                                                       < EmployeeDetails >("exec GetEmployeeData ", null).ToList();
}

DbContext also has some sets of methods that are not available with ObjectContext, like Dbset. Find, DbSet.Local etcetera.

Some of the ObjectContext methods are also renamed. For example, ObjectContext has methods like AddObject, DeleteObject, And Attach on ObjectSet<T>, in DbContext, they are named Add, Remove and Attach on DbSet<TEntity>.

Conclusion

Dbcontext can be defined as a lightweight version of the ObjectContext or we can say Dbcontext is a wrapper of ObjectContext and exposes only the common features that are really required in programming.

We can also get a reference to the ObjectContext from then DbContext to use those features that are only supported in ObjectContext.

The following code could help to get an ObjectContext Object from an existing DbContext Object.

public class EntityDBContext: DbContext, 
IObjectContextAdapter {    
ObjectContext IObjectContextAdapter.ObjectContext    
{         get         {               var objectContext = (this as IObjectContextAdapter)               if(objectContext != null)                 
return (this as IObjectContextAdapter).ObjectContext;
              else                 return null;
        }    } }

Finally, DbContext is not a replacement for ObjectContext, but it is a simple alternative that builds on ObjectContext.

Hope this will help to understand the differences between ObjectContext and DbContext.


Similar Articles