为什么实体框架映射使用ADO.NET实体数据模型的数据库表时自动使用的ObjectContext代替的DbContext的 [英] Why does Entity Framework automatically use the ObjectContext instead of the DbContext when mapping database tables using ADO.NET Entity datamodel

查看:367
本文介绍了为什么实体框架映射使用ADO.NET实体数据模型的数据库表时自动使用的ObjectContext代替的DbContext的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我先上后下的数据库的方法;我在我的SQL Server 2008数据库中创建的表,然后映射那些表使用ADO.NET实体数据模型实体框架类。但是,当我打开designer.cs文件,我发现在类定义如下code这是自动创建的:

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:

public partial class PortalEntities : ObjectContext

所以我有以下三个疑问,让我困惑:

so I have the following three question that get my confused:


  1. 为什么我的 PortalEntities 类从的ObjectContext ,而不是派生的DbContext 我期待?

有没有之间的主要区别的ObjectContext &安培; 的DbContext ,或者他们主要是相同的,并提供相同的功能

Is there a major difference between ObjectContext & DbContext, or they are mainly the same and offer that same capabilities

当我尝试写类似下面的code中的东西:

When I try to write the something similar to the following code:

Student student = db.Students.Find(id);


我发现,我以前用做我不能使用 .Find()方法的DbContext ,这样做这意味着的ObjectContext &安培; 的DbContext 有,我可以用不同的方法?

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?

BR

推荐答案

的DbContext 是围绕一个包装的的ObjectContext 简化了界面,我们大多数做的事情。

The DbContext is a wrapper around the ObjectContext which simplifies the interface for the things we do most.

如果你有一个的DbContext ,你仍然可以访问的ObjectContext ((IObjectContextAdapter )的DbContext).ObjectContext;

If you have an DbContext you can still access the ObjectContexttrough ((IObjectContextAdapter)dbContext).ObjectContext;

如果你想使用而不是的DbContext 第一次使用数据库时的ObjectContext ,您可以切换模板这是用于生成您的code。您可以通过右键点击你的EDMX做到这一点,选择添加code代项目。然后,您可以选择的DbContext模板。

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.

这里是整个过程的一个例子。

Here is an example of the whole process.

这篇关于为什么实体框架映射使用ADO.NET实体数据模型的数据库表时自动使用的ObjectContext代替的DbContext的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆