设计器代码生成模板Designer Code Generation Templates

使用 Entity Framework 设计器创建模型时,会自动生成类和派生上下文。When you create a model using the Entity Framework Designer your classes and derived context are automatically generated for you. 除默认代码生成之外,我们还提供了许多模板,可用于自定义生成的代码。In addition to the default code generation we also provide a number of templates that can be used to customize the code that gets generated. 这些模板以 T4 文本模板的形式提供,可按需自定义模板。These templates are provided as T4 Text Templates, allowing you to customize the templates if needed.

默认生成的代码取决于创建模型的 Visual Studio 版本:The code that gets generated by default depends on which version of Visual Studio you create your model in:

  • 在 Visual Studio 2012 和 2013 中创建的模型将生成简单的 POCO 实体类和派生的简化 DbContext 的上下文。Models created in Visual Studio 2012 & 2013 will generate simple POCO entity classes and a context that derives from the simplified DbContext.
  • 在 Visual Studio 2010 中创建的模型将生成派生自 EntityObject 的实体类和派生自 ObjectContext 的上下文。Models created in Visual Studio 2010 will generate entity classes that derive from EntityObject and a context that derives from ObjectContext.

备注

建议在添加模型后切换到 DbContext 生成器模板。We recommend switching to the DbContext Generator template once you've added your model.

此页介绍了可用的模板,并说明了如何将模板添加到模型。This page covers the available templates and then provides instructions for adding a template to your model.

可用模板Available Templates

以下模板由实体框架团队提供:The following templates are provided by the Entity Framework team:

DbContext 生成器DbContext Generator

此模板将生成简单的 POCO 实体类和派生自使用 EF6 的 DbContext 的上下文。This template will generate simple POCO entity classes and a context that derives from DbContext using EF6. 除有需要使用以下列出的其他模板外,推荐使用此模板。This is the recommended template unless you have a reason to use one of the other templates listed below. 如果使用的是最新版本的 Visual Studio(Visual Studio 2013 以上版本),则也默认使用代码生成模板:创建新模型时,默认使用此模板,且 T4 文件 (.tt) 会嵌套在 .edmx 文件下。It is also the code generation template you get by default if you are using recent versions of Visual Studio (Visual Studio 2013 onwards): When you create a new model this template is used by default and the T4 files (.tt) are nested under your .edmx file.

较旧版本的 Visual StudioOlder versions of Visual Studio

  • Visual Studio 2012: 若要获取 EF 6.x DbContextGenerator 模板,则需安装适用于 Visual Studio 的最新 Entity Framework Tools - 请参阅获取实体框架页以了解详细信息********。Visual Studio 2012: To get the EF 6.x DbContextGenerator templates you will need to install the latest Entity Framework Tools for Visual Studio - see the Get Entity Framework page for more information.
  • Visual Studio 2010: EF 6.x DbContextGenerator 模板不适用于 Visual Studio 2010****。Visual Studio 2010: The EF 6.x DbContextGenerator templates are not available for Visual Studio 2010.

EF 5.x 的 DbContext 生成器DbContext Generator for EF 5.x

如果使用的是较旧版本的 EntityFramework NuGet 包(具有主要版本 5),则需使用 EF 5.x DbContext 生成器模板****。If you are using an older version of the EntityFramework NuGet package (one with a major version of 5) you will need to use the EF 5.x DbContext Generator template.

如果使用的是 Visual Studio 2013 或 2012,则其中已安装该模板。If you are using Visual Studio 2013 or 2012 this template is already installed.

如果使用的是 Visual Studio 2010,添加模板时,则需选择“联机”选项卡,从 Visual Studio 库中下载模板****。If you are using Visual Studio 2010 you will need to select the Online tab when adding the template to download it from Visual Studio Gallery. 或可直接从 Visual Studio 库提前安装模板。Alternatively you can install the template directly from Visual Studio Gallery ahead of time. 因为模板包含在较新版本的 Visual Studio 中,因此只能将库上的版本安装在 Visual Studio 2010 中。Because the templates are included in later versions of Visual Studio the versions on the gallery can only be installed on Visual Studio 2010.

EF 4.x 的 DbContext 生成器DbContext Generator for EF 4.x

如果使用的是较旧版本的 EntityFramework NuGet 包(具有主要版本 4),则需使用 EF 4.x DbContext 生成器模板****。If you are using an older version of the EntityFramework NuGet package (one with a major version of 4) you will need to use the EF 4.x DbContext Generator template. 添加模板时,可在“联机”选项卡中找到此模板,或可直接从 Visual Studio 库提前安装模板****。This can be found in the Online tab when adding the template, or you can install the template directly from Visual Studio Gallery ahead of time.

EntityObject 生成器EntityObject Generator

此模板将生成从 EntityObject 派生的实体类和从 ObjectContext 派生的上下文。This template will generate entity classes that derive from EntityObject and a context that derives from ObjectContext.

备注

请考虑使用 DbContext 生成器Consider using the DbContext Generator

现推荐新应用程序使用 DbContext 生成器模板。The DbContext Generator is now the recommended template for new applications. DbContext 生成器利用了更简单的 DbContext API。The DbContext Generator takes advantage of the simpler DbContext API. EntityObject 生成器仍支持用于现有应用程序。The EntityObject Generator continues to be available to support existing applications.

Visual Studio 2010、2012 和 2013Visual Studio 2010, 2012 & 2013

添加模板时,需选择“联机”选项卡,从 Visual Studio 库中下载模板****。You will need to select the Online tab when adding the template to download it from Visual Studio Gallery. 或可直接从 Visual Studio 库提前安装模板。Alternatively you can install the template directly from Visual Studio Gallery ahead of time.

EF 5.x 的 EntityObject 生成器EntityObject Generator for EF 5.x

如果使用的是 Visual Studio 2012 或 2013,添加模板时,则需选择“联机”选项卡,从 Visual Studio 库中下载模板****。If you are using Visual Studio 2012 or 2013 you will need to select the Online tab when adding the template to download it from Visual Studio Gallery. 或可直接从 Visual Studio 库提前安装模板。Alternatively you can install the template directly from Visual Studio Gallery ahead of time. 因为模板包含在 Visual Studio 2010 中,因此只能将库上的模板安装在 Visual Studio 2012 和 2013 中。Because the templates are included in Visual Studio 2010 the versions on the gallery can only be installed on Visual Studio 2012 & 2013.

如果只需要生成 ObjectContext 代码,无需编辑模板,则可还原为 EntityObject 代码生成If you just want ObjectContext code generation without needing to edit the template you can revert to EntityObject code generation.

如果使用的是 Visual Studio 2010,则其中已安装该模板。If you are using Visual Studio 2010 this template is already installed. 如果在 Visual Studio 2010 中创建新模型,则默认使用此模板,但项目中不包含 .tt 文件。If you create a new model in Visual Studio 2010 this template is used by default but the .tt files are not included in your project. 若想自定义模板,则需将其添加到项目中。If you want to customize the template you will need to add it to your project.

自跟踪实体 (STE) 生成器Self-Tracking Entities (STE) Generator

此模板将生成自跟踪实体类和从 ObjectContext 派生的上下文。This template will generate Self-Tracking Entity classes and a context that derives from ObjectContext. 在 EF 应用程序中,上下文负责跟踪实体中的更改。In an EF application, a context is responsible for tracking changes in the entities. 但是,在 N 层方案中,上下文可能无法用于修改实体的层。However, in N-Tier scenarios, the context might not be available on the tier that modifies the entities. 自跟踪实体有助于跟踪任意层中的更改。Self-tracking entities help you track changes in any tier. 有关详细信息,请参阅自跟踪实体For more information, see Self-Tracking Entities.

备注

不推荐使用 STE 模板STE Template Not Recommended

不再建议在新应用程序中使用 STE 模板,虽然该模板仍支持用于现有应用程序。We no longer recommend using the STE template in new applications, it continues to be available to support existing applications. 请查看断开连接的实体文章了解推荐用于 N 层方案的其他选项。Visit the disconnected entities article for other options we recommend for N-Tier scenarios.

备注

没有 EF 6.x 版的 STE 模板。There is no EF 6.x version of the STE template.

备注

没有 Visual Studio 2013 版的 STE 模板。There is no Visual Studio 2013 version of the STE template.

Visual Studio 2012Visual Studio 2012

如果使用的是 Visual Studio 2012,添加模板时,则需选择“联机”选项卡,从 Visual Studio 库中下载模板****。If you are using Visual Studio 2012 you will need to select the Online tab when adding the template to download it from Visual Studio Gallery. 或可直接从 Visual Studio 库提前安装模板。Alternatively you can install the template directly from Visual Studio Gallery ahead of time. 因为模板包含在 Visual Studio 2010 中,因此只能将库上的模板安装在 Visual Studio 2012 中。Because the templates are included in Visual Studio 2010 the versions on the gallery can only be installed on Visual Studio 2012.

Visual Studio 2010**Visual Studio 2010**

如果使用的是 Visual Studio 2010,则其中已安装该模板。If you are using Visual Studio 2010 this template is already installed.

POCO 实体生成器POCO Entity Generator

此模板将生成 POCO 实体类和从 ObjectContext 派生的上下文This template will generate POCO entity classes and a context that derives from ObjectContext

备注

请考虑使用 DbContext 生成器Consider using the DbContext Generator

现推荐使用 DbContext 生成器模板在新应用程序中生成 POCO 类。The DbContext Generator is now the recommended template for generating POCO classes in new applications. DbContext 生成器利用了新的 DbContext API,且可生成更简单的 POCO 类。The DbContext Generator takes advantage of the new DbContext API and can generate simpler POCO classes. POCO 实体生成器仍支持用于现有应用程序。The POCO Entity Generator continues to be available to support existing applications.

备注

没有EF 5.x 或 EF 6.x 版的 STE 模板。There is no EF 5.x or EF 6.x version of the STE template.

备注

没有 Visual Studio 2013 版的 POCO 模板。There is no Visual Studio 2013 version of the POCO template.

Visual Studio 2012 和 Visual Studio 2010Visual Studio 2012 & Visual Studio 2010

添加模板时,需选择“联机”选项卡,从 Visual Studio 库中下载模板****。You will need to select the Online tab when adding the template to download it from Visual Studio Gallery. 或可直接从 Visual Studio 库提前安装模板。Alternatively you can install the template directly from Visual Studio Gallery ahead of time.

“网站”模板是什么What are the "Web Sites" Templates

“网站”模板(例如适用于 C# 网站的 EF 5.x DbContext 生成器)可用于通过“文件”->“新建”->“网站...”创建的网站项目 。这不同于通过“文件”->“新建”->项目...”创建的 Web 应用程序,后者使用的是标准模板。The "Web Sites" templates (for example, EF 5.x DbContext Generator for C# Web Sites) are for use in Web Site projects created via File -> New -> Web Site.... These are different from Web Applications, created via File -> New -> Project..., which use the standard templates. 我们单独提供了这些模板,因为 Visual Studio 中的项模板系统需要这些模板。We provide separate templates because the item template system in Visual Studio requires them.

使用模板Using a Template

若要开始使用代码生成模板,请右键单击 EF 设计器中设计界面上的空白区域,然后选择“添加代码生成项...”****。To start using a code generation template, right-click an empty spot on the design surface in the EF Designer and select Add Code Generation Item....

添加代码生成项

如果已安装要使用的模板(或模板包含在 Visual Studio 中),则可从左侧菜单的“代码”或“数据”部分使用此模板********。If you've already installed the template you want to use (or it was included in Visual Studio), then it will be available under either the Code or Data section from the left menu.

安装的模板

如果尚未安装模板,请选择左侧菜单中的“联机”并搜索所需模板****。If you don't already have the template installed, select Online from the left menu and search for the template you want.

搜索模板 

如果使用的是 Visual Studio 2012,则新的 .tt 文件将嵌套在 .edmx 文件下。*If you are using Visual Studio 2012, the new .tt files will be nested under the .edmx file.*

备注

对于在 Visual Studio 2012 中创建的模型,需删除用于默认代码生成的模板,否则会生成重复的类和上下文。For models created in Visual Studio 2012 you will need to delete the templates used for default code generation, otherwise you will have duplicate classes and context generated. 默认文件是 <model name>.tt 和 <model name>.context.tt 。The default files are <model name>.tt and <model name>.context.tt. 

VS2012 模板

如果使用的是 Visual Studio 2010,则 tt 文件会直接添加到项目中。If you are using Visual Studio 2010, the tt files are added directly to your project.  

VS2010 模板