恢复到 Entity Framework Designer 中的 ObjectContextReverting to ObjectContext in Entity Framework Designer

使用早期版本的实体框架使用 EF 设计器创建的模型将生成派生自 EntityObject 的 ObjectContext 和实体类的上下文。With previous version of Entity Framework a model created with the EF Designer would generate a context that derived from ObjectContext and entity classes that derived from EntityObject.

从 EF 4.1 开始,我们建议换到代码生成模板,该模板生成派生自 DbContext 和 POCO 实体类的上下文。Starting with EF4.1 we recommended swapping to a code generation template that generates a context deriving from DbContext and POCO entity classes.

在 Visual Studio 2012 中,会为使用 EF 设计器创建的所有新模型获取默认生成的 DbContext 代码。In Visual Studio 2012 you get DbContext code generated by default for all new models created with the EF Designer. 现有模型将继续生成基于 ObjectContext 的代码,除非你决定要切换到基于 DbContext 的代码生成器。Existing models will continue to generate ObjectContext based code unless you decide to swap to the DbContext based code generator.

恢复到 ObjectContext 代码生成Reverting Back to ObjectContext Code Generation

1. 禁用 DbContext 代码生成1. Disable DbContext Code Generation

派生的 DbContext 和 POCO 类的生成由项目中的两个 tt 文件处理,如果在 "解决方案资源管理器" 中展开 .edmx 文件,则会看到这些文件。Generation of the derived DbContext and POCO classes is handled by two .tt files in you project, if you expand the .edmx file in solution explorer you will see these files. 从项目中删除这两个文件。Delete both of these files from your project.

代码生成文件

如果你使用的是 VB.NET,则需要选择 " 显示所有文件 " 按钮以查看嵌套的文件。If you are using VB.NET you will need to select the Show All Files button to see the nested files.

显示所有文件

2. 重新启用 ObjectContext 代码生成2. Re-Enable ObjectContext Code Generation

在 EF 设计器中打开模型,右键单击设计图面的空白部分,然后选择 " 属性"。Open you model in the EF Designer, right-click on a blank section of the design surface and select Properties.

在属性窗口将 代码生成策略None 更改为 默认值In the Properties window change the Code Generation Strategy from None to Default.

代码生成策略