.net 实体框架中的“ObjectContext”与“DbContext”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9176967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'ObjectContext' vs 'DbContext' in Entity Framework
提问by afr0
I'm using the DbContextclass within code that I am creating that is based on the Generic Repositories and Unit of Work design patterns. (I am following the guidance here.) While working on this project I have encountered the ObjectContextclass.
我正在DbContext创建的代码中使用该类,该类基于通用存储库和工作单元设计模式。(我正在遵循此处的指导。)在处理这个项目时,我遇到了这ObjectContext门课。
I've read quite a number of posts that discuss ObjectContextvs. DbContext. While some of what I've read makes sense, I still don't have a complete understanding of the differences and this leaves me wondering about my current implementation. Should I be using DbContext, ObjectContextor both? Is using one of these now considered an anti-pattern?
我已经阅读了很多讨论ObjectContextvs.DbContext的帖子。虽然我读过的一些内容是有道理的,但我仍然没有完全理解这些差异,这让我对我当前的实现感到疑惑。我应该使用DbContext,ObjectContext还是两者都使用?使用其中一个现在被认为是反模式吗?
回答by Massimiliano Peluso
DbContextis just a wrapper around ObjectContext.
DbContext只是一个包装ObjectContext。
DbContextis just a set of APIs that are easier to use than the APIs exposed by ObjectContext.
DbContext只是一组比ObjectContext.
Anyway, hereyou'll find a very simple Visual Studio template that uses the Repository Pattern and the Entity Framework.
不管怎样,在这里你会发现一个非常简单的 Visual Studio 模板,它使用了存储库模式和实体框架。
回答by Hui Zhao
From ObjectContext VS DBContext.
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.
Dbcontext 可以定义为 ObjectContext 的轻量级版本,或者我们可以说 Dbcontext 是 ObjectContext 的包装器,并且仅公开编程中真正需要的公共功能。我们还可以从 DbContext 中获取对 ObjectContext 的引用,以使用仅在 ObjectContext 中支持的那些功能。
The following code could help to get an ObjectContext Object from an existing DbContext Object.
以下代码有助于从现有 DbContext 对象获取 ObjectContext 对象。
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.
最后,DbContext 不是 ObjectContext 的替代品,而是构建在 ObjectContext 之上的简单替代品。
回答by Balpreet_1988
We can cast a DBContext to type ObjectContext
我们可以将 DBContext 类型转换为 ObjectContext
public class MyContext: DbContext
{
public DbSet<Blog> Blogs { get; set; }
//other dbsets, ctor etc.
public ObjectContext ObjectContext()
{
return (this as IObjectContextAdapter).ObjectContext;
}
}

