As a brief recap, the reason this post even exists is because the generated code from the EF4 designer does not properly suppress code analysis warnings. In that post, I mentioned that I would provide a proper T4 template and tutorial on integrating it into your EDMX model. This post details the steps to creating and customizing an Entity Framework 4 T4 template.

T4 (the Text Template Transformation Toolkit) is the code generation technology built into Visual Studio. Thankfully, the EF4 designer has been enhanced to support customizable templates so that developers are free to change the code that is generated for your ObjectContext and Entity classes.

To get started, I am assuming you already have an EDMX file that has been generated from your database.

Beta Note

These steps were written for Visual Studio 2010 Beta 2, so they may change slightly before RTM.

Step 1

Open the EDMX Designer, right-click on the design surface, and select “Add Code Generation Item…”

addcodegenitem 

Step 2

Select the “ADO.NET EntityObject Generator” from the Add New Item dialog

Note: The specific instructions below only work for the standard EntityObject Generator, and will not apply directly to the Self-Tracking Entities template. If you wish to use Self-Tracking entities, the instructions in this post should be sufficient to customize the template yourself.

image

Now you should have a new EfModel.tt file in your project. This is the T4 template that the EDMX designer will use to generate the classes that represent your EDMX model.

Step 3

Open the newly created EfModel.tt file.

From here, you can customize the template yourself (instructions are provided in Step 3b, or simply download the T4 template that I have provided in Step 3a)

 

Step 3a – Download the corrected template

  1. Open the T4 Template
  2. Overwrite all text in your .tt file with the text from the link above
  3. Change the token “YOUR_EDMX_FILE.edmx” to be the file name of you actual edmx file
  4. Proceed to step 4

 

Step 3b - Customize your template manually

To suppress code analysis, you need to decorate each class definition with a [GeneratedCode] attribute, as follows:

      [global::System.CodeDom.Compiler.GeneratedCode("EdmxTool", "1.0.0.0")]

You can use the standard Visual Studio Find dialog to find all instances where classes are being generated. In the Template that I am using (The one that shipped with Visual Studio 2010 Beta 2) there were 3 places to decorate the code.

  1. The ObjectContext class
  2. The Entity classes
  3. The ComplexType classes

Following is a snippet from my modified ObjectContext generation.

/// <summary>
/// <#=SummaryComment(container)#>
/// </summary><#=LongDescriptionCommentElement(container, region.CurrentIndentLevel)#>
[global::System.CodeDom.Compiler.GeneratedCode("EdmxTool", "1.0.0.0")]
<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : ObjectContext

Repeat this process for the 3 remaining class definitions by using the standard Find… dialog. I found it easiest to search for the text “partial class”

Step 4

Open the EDMX designer and Ctrl+S to save it.

If all goes correctly, the T4 template should kick into action and a new .cs file should be created. This .cs file includes the actual generated code based on your EDMX model and your T4 template. As always with generated code don’t attempt to modify it directly, since the next time you (or someone else) saves the edmx file it will overwrite any changes you made.

Lastly, one final benefit of all of this is that you can make any changes in the T4 template to fully customize how your entities are generated from now on.

 

Technorati Tags: ,

Comments

# re: Customizing the Entity Framework T4 Template, suppressing code analysis

Avatar
Thomas Scheelhardt
2/28/2010 11:06 AM
Hi,

Unfortunately there is a caveat with this approach.
Marking partial classes as GeneratedCode has the effect that any customizations you may add will also be affected by the attribute and hence not be covered by code analysis or metrics.

# re: Customizing the Entity Framework T4 Template, suppressing code analysis

Avatar
Cs Hacks
4/16/2010 6:32 AM
We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.

Thank you,

# re: Customizing the Entity Framework T4 Template, suppressing code analysis

Avatar
Kasino Rezensionen
6/15/2010 7:05 AM
You made a design decision to not follow established naming guidelines. Fine, I will assume you had good reasons for that decision. But then there is NO excuse for not including the GeneratedCode attribute by default. The template exists to generate code; if there is code that needs to be generated (like an attribute), it needs to be in the template. "Clutter" is not an issue......

# ...

Avatar
Discount Furniture
6/18/2010 9:49 PM
Of course, what a great site and informative posts with furniture, I will bookmark this site.

# re: Customizing the Entity Framework T4 Template, suppressing code analysis

Avatar
Agile Informatics
7/19/2010 2:53 AM
The generated code can not follow all the fxcop guidelines because of other requirements( mainly around Serialization). The guidance here is to put GeneratedCode attribute on the members of the generated code so that FXCop does not flag these errors. In .Net 4.0( which is our next release and Beta 1 of which is available now), we moved to use T4 templates for code generation.

# re: Customizing the Entity Framework T4 Template, suppressing code analysis

Avatar
casino en ligne
7/29/2010 4:23 AM
I'm trying to run codeanalysis on an assembly that contains an entity model (edmx file). In project properties I have checked the "Suppress results from generated code" option, but I am still getting a lot of CA errors pertaining to the auto-generated EF code.....

# re: Customizing the Entity Framework T4 Template, suppressing code analysis

Avatar
Vahid
8/7/2010 3:33 AM
Greatttttttttttttttttttttttttttt.
Greatttttttttttttttttttttttttttt.
I become your bolog permanent reader
Comments have been closed on this topic.