I'm using the DbContext
class 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 ObjectContext
class.
我正在使用基于Generic Repositories和Unit of Work设计模式创建的代码中的DbContext类。 (我在这里遵循指导。)在处理这个项目时,我遇到了ObjectContext类。
I've read quite a number of posts that discuss ObjectContext
vs. 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
, ObjectContext
or both? Is using one of these now considered an anti-pattern?
我已经阅读了很多讨论ObjectContext与DbContext的帖子。虽然我读过的一些内容是有道理的,但我仍然没有完全理解这些差异,这使我对我当前的实现感到疑惑。我应该使用DbContext,ObjectContext还是两者?使用其中一个现在被认为是反模式?
3 个解决方案
#1
21
DbContext
is just a wrapper around ObjectContext
.
DbContext只是ObjectContext的包装器。
DbContext
is just a set of APIs that are easier to use than the APIs exposed by ObjectContext
.
DbContext只是一组比ObjectContext公开的API更容易使用的API。
Anyway, here you'll find a very simple Visual Studio template that uses the Repository Pattern and the Entity Framework.
无论如何,在这里,您将找到一个使用存储库模式和实体框架的非常简单的Visual Studio模板。
#2
-1
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;
}
}
#3
-1
From ObjectContext VS DBContext.
来自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上的简单替代方案。
#1
21
DbContext
is just a wrapper around ObjectContext
.
DbContext只是ObjectContext的包装器。
DbContext
is just a set of APIs that are easier to use than the APIs exposed by ObjectContext
.
DbContext只是一组比ObjectContext公开的API更容易使用的API。
Anyway, here you'll find a very simple Visual Studio template that uses the Repository Pattern and the Entity Framework.
无论如何,在这里,您将找到一个使用存储库模式和实体框架的非常简单的Visual Studio模板。
#2
-1
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;
}
}
#3
-1
From ObjectContext VS DBContext.
来自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上的简单替代方案。