I am following the database approach first; I have created the tables in my SQL Server 2008 database, then I map those tables to Entity Framework classes using an ADO.NET Entity Data Model. But when I opened the designer.cs file I found the following code in the class definition which was created automatically:
我首先采用的是数据库方法;我在SQL Server 2008数据库中创建了表,然后使用ADO将这些表映射到实体框架类。网络实体数据模型。但当我打开设计师。cs文件我在类定义中找到了以下代码,它是自动创建的:
public partial class PortalEntities : ObjectContext
so I have the following three question that get my confused:
所以我有以下三个问题让我困惑:
-
Why does my
PortalEntities
class derive fromObjectContext
and notDbContext
as I was expecting?为什么我的PortalEntities类派生自ObjectContext,而不是像我预期的那样派生DbContext ?
-
Is there a major difference between
ObjectContext
&DbContext
, or they are mainly the same and offer that same capabilitiesObjectContext和DbContext之间是否存在重大差异,或者它们主要是相同的并提供相同的功能
-
When I try to write the something similar to the following code:
当我试图写类似于以下代码的东西时:
Student student = db.Students.Find(id);
I found that I cannot use .Find()
method as I used to do using DbContext
, so does this mean that ObjectContext
& DbContext
have different methods that I can use?
我发现我不能像以前使用DbContext那样使用。find()方法,这是否意味着ObjectContext和DbContext有不同的方法可以使用?
BR
BR
2 个解决方案
#1
24
The DbContext
is a wrapper around the ObjectContext
which simplifies the interface for the things we do most.
DbContext是ObjectContext的包装器,它简化了我们所做的事情的接口。
If you have an DbContext
you can still access the ObjectContext
trough ((IObjectContextAdapter)dbContext).ObjectContext;
如果您有一个DbContext,您仍然可以访问objectcontext槽((IObjectContextAdapter) DbContext).ObjectContext;
If you want to use the DbContext
instead of the ObjectContext
when using database first, you can switch the template that's used for generating your code. You can do this by right-clicking in your EDMX and selecting 'Add Code Generation Item'. You can then select the DbContext template.
如果您想在使用数据库时使用DbContext而不是ObjectContext,那么可以切换用于生成代码的模板。可以通过在EDMX中右键单击并选择“添加代码生成项”来实现这一点。然后可以选择DbContext模板。
Here is an example of the whole process.
这是整个过程的一个例子。
#2
0
Since VS2012 the default code generation changed from ObjectContext
to DbContext
.
自VS2012以来,默认的代码生成从ObjectContext更改为DbContext。
#1
24
The DbContext
is a wrapper around the ObjectContext
which simplifies the interface for the things we do most.
DbContext是ObjectContext的包装器,它简化了我们所做的事情的接口。
If you have an DbContext
you can still access the ObjectContext
trough ((IObjectContextAdapter)dbContext).ObjectContext;
如果您有一个DbContext,您仍然可以访问objectcontext槽((IObjectContextAdapter) DbContext).ObjectContext;
If you want to use the DbContext
instead of the ObjectContext
when using database first, you can switch the template that's used for generating your code. You can do this by right-clicking in your EDMX and selecting 'Add Code Generation Item'. You can then select the DbContext template.
如果您想在使用数据库时使用DbContext而不是ObjectContext,那么可以切换用于生成代码的模板。可以通过在EDMX中右键单击并选择“添加代码生成项”来实现这一点。然后可以选择DbContext模板。
Here is an example of the whole process.
这是整个过程的一个例子。
#2
0
Since VS2012 the default code generation changed from ObjectContext
to DbContext
.
自VS2012以来,默认的代码生成从ObjectContext更改为DbContext。