Suppressing Code Analysis warnings in Entity Framework generated code

When using Entity Framework Database First it can happen that the code generated by the edmx file causes Code Analysis errors. This happens due to the fact that the t4-template misses the GeneratedCode attribute. It is pretty easy to modify the template to contain the necessary attributes.

Modify the context template

  1. Add using System.CodeDom.Compiler; at the right position in the using directives
  2. Add [GeneratedCode(“EntityModelCodeGenerator”, “4.0.0.0”)] above the class declaration line

Modify the template of the model classes

  1. Find EntityClassOpening and modify to the return a string starting with the GeneratedCode attribute
  2. Find <#=codeStringGenerator.UsingDirectives(inHeader: false)#> and add  below using System.CodeDom.Compiler;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s