ObjectContext && DbContext

本文深入探讨了ObjectContext与DbContext之间的区别,两者都是Entity Framework中用于处理数据的重要组件。DbContext被视为ObjectContext的轻量级替代品,更适合现代开发流程,尤其是在Code First方法中。文章详细解释了它们在功能、使用场景及Entity Framework版本支持上的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源:http://www.c-sharpcorner.com/UploadFile/ff2f08/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 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

ObjectContext

DbContext

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.

ObjectContext supports Compiled Queries.

DbContext does not support Compiled Queries.

ObjectContext supports self-tracking of Entities

DbContext does not support self-tracking of Entities.

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.

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 Attech 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 of ObjectContext, but it is a simple alternative that builds on ObjectContext. 

转载于:https://www.cnblogs.com/clarkwei/p/4045068.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值