Redaguoti

Bendrinti naudojant


Designer Code Generation Templates

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. These templates are provided as T4 Text Templates, allowing you to customize the templates if needed.

The code that gets generated by default depends on which version of Visual Studio you create your model in:

  • Models created in Visual Studio 2012 & 2013 will generate simple POCO entity classes and a context that derives from the simplified DbContext.
  • Models created in Visual Studio 2010 will generate entity classes that derive from EntityObject and a context that derives from ObjectContext.

Note

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 Generator

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. 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.

Older versions of Visual Studio

  • 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: The EF 6.x DbContextGenerator templates are not available for Visual Studio 2010.

DbContext Generator for EF 5.x

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.

If you are using Visual Studio 2013 or 2012 this template is already installed.

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. Alternatively you can install the template directly from Visual Studio Gallery ahead of time. Because the templates are included in later versions of Visual Studio the versions on the gallery can only be installed on Visual Studio 2010.

DbContext Generator for EF 4.x

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. 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 Generator

This template will generate entity classes that derive from EntityObject and a context that derives from ObjectContext.

Note

Consider using the DbContext Generator

The DbContext Generator is now the recommended template for new applications. The DbContext Generator takes advantage of the simpler DbContext API. The EntityObject Generator continues to be available to support existing applications.

Visual Studio 2010, 2012 & 2013

You will need to select the Online tab when adding the template to download it from Visual Studio Gallery. Alternatively you can install the template directly from Visual Studio Gallery ahead of time.

EntityObject Generator for EF 5.x

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. Alternatively you can install the template directly from Visual Studio Gallery ahead of time. Because the templates are included in Visual Studio 2010 the versions on the gallery can only be installed on Visual Studio 2012 & 2013.

If you just want ObjectContext code generation without needing to edit the template you can revert to EntityObject code generation.

If you are using Visual Studio 2010 this template is already installed. 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.

Self-Tracking Entities (STE) Generator

This template will generate Self-Tracking Entity classes and a context that derives from ObjectContext. In an EF application, a context is responsible for tracking changes in the entities. 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.

Note

STE Template Not Recommended

We no longer recommend using the STE template in new applications, it continues to be available to support existing applications. Visit the disconnected entities article for other options we recommend for N-Tier scenarios.

Note

There is no EF 6.x version of the STE template.

Note

There is no Visual Studio 2013 version of the STE template.

Visual Studio 2012

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. Alternatively you can install the template directly from Visual Studio Gallery ahead of time. 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**

If you are using Visual Studio 2010 this template is already installed.

POCO Entity Generator

This template will generate POCO entity classes and a context that derives from ObjectContext

Note

Consider using the DbContext Generator

The DbContext Generator is now the recommended template for generating POCO classes in new applications. The DbContext Generator takes advantage of the new DbContext API and can generate simpler POCO classes. The POCO Entity Generator continues to be available to support existing applications.

Note

There is no EF 5.x or EF 6.x version of the STE template.

Note

There is no Visual Studio 2013 version of the POCO template.

Visual Studio 2012 & Visual Studio 2010

You will need to select the Online tab when adding the template to download it from Visual Studio Gallery. Alternatively you can install the template directly from Visual Studio Gallery ahead of time.

What are the "Web Sites" Templates

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. We provide separate templates because the item template system in Visual Studio requires them.

Using a Template

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....

Add Code Gen Item

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.

Installed Template

If you don't already have the template installed, select Online from the left menu and search for the template you want.

Search Template 

If you are using Visual Studio 2012, the new .tt files will be nested under the .edmx file.*

Note

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. The default files are <model name>.tt and <model name>.context.tt

VS2012 Templates

If you are using Visual Studio 2010, the tt files are added directly to your project.  

VS2010 Templates