How to: Customize Object-Layer Code Generation (Entity Data Model Designer)
This topic describes how to use the ADO.NET Entity Data Model Designer (Entity Designer) and text templates to customize the generation of object context and entity classes (object-layer code).
By default, the Entity Designer uses the EntityModelCodeGenerator custom tool to generate object-layer code (for more information, see Generated Code Overview (Entity Data Model Designer)). When you add a text template to a project, the Entity Designer can use the template to generate object-layer code instead of using the default custom tool.
Note |
---|
You can use command-line tools to generate object-layer code with a text template. For more information, see Command-Line Tools for Text Templates. |
Visual Studio provides two ADO.NET templates that generate the Entity Framework object layer code: the ADO.NET EntityObject Generator and the ADO.NET Self-Tracking Entity Generator. The ADO.NET EntityObject Generator generates the same code as the EntityModelCodeGenerator custom tool. The ADO.NET Self-Tracking Entity Generator template generates entity classes that contain self-tracking state logic. Use self-tracking entities when working with N-tier applications. For more information about ADO.NET templates, see ADO.NET EntityObject Generator Template and ADO.NET Self-Tracking Entity Generator Template.
The procedure below assumes the following:
You have a Visual Basic or C# project that targets the .NET Framework 4 open in Visual Studio 2010.
You have an .edmx file open in the Entity Designer.
You are familiar with customizing text templates. For information about text templates, see Generating Artifacts By Using Text Templates.
To customize object-layer code generation
Right-click an empty area on the Entity Designer surface, point to Add Code Generation Item, and select an available template. If no templates appear to be available, select Code in the Installed Templates pane.
The code generation item adds one or more text template (.tt) files to the project. The text templates generate object-layer code. The generated files are added under the .tt files. If you do not modify this template, the generated code will be the same as the code generated by the Entity Designer.
When you add the text template to your project, Visual Studio does the following:
Sets the Code Generation Strategy property of the conceptual model to None. This turns off the default code generation.
Adds a new text template file to the project in the same directory as the .edmx file.
Inserts the .edmx file path relative to the text template into the text template. This is done so that the text template will process the correct .edmx file.
Uses the text template to generate code based on the .edmx file. The generated code is written to a
<text_template_name>.cs
or<text_template_name>.vb
file. The file is added as a dependent file of the template file in Solution Explorer.
Open the text template and edit it.
Save the edited text template file.
When you save the text template file, object-layer code is re-generated according to the edited text template file.
By default, object-layer code generation for an .edmx file is triggered any time you save the .edmx file. Any manual changes made to the <text_template_name>.cs
or <text_template_name>.vb
code files are not preserved. If an .edmx file has more than one text template file associated with it, all associated code files are re-generated when the .edmx file is saved. However, this behavior can be changed. If you set the Transform Related Text Templates On Save property of the conceptual model to false, code files will not be re-generated when the file is saved.
When you split the model and the .tt files into separate projects, you should manually run the code generation whenever you modify an .edmx file or a .tt file. Do this by clicking Transform All Templates button in Solution Explorer. The Walkthrough: Serialize Self-Tracking Entities (Entity Framework) topic demonstrates how to split the model and the .tt files into separate projects.
See Also
Concepts
.edmx File Overview (Entity Framework)