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.
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. Two templates are available by default: ADO.NET EntityObject Generator and the ADO.NET Self-Tracking Entity Generator. The ADO.NET EntityObject Generator generates the same code that is the default code generated by the Entity Designer. The ADO.NET Self-Tracking Entity Generator template generates entity classes that contain self-tracking state logic. For more information see Walkthrough: Serialize Self-Tracking Entities (Entity Framework).
A text template (a .tt file) that generates object-layer code in the project language is added to the project. 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.
See Also