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”, “”)] 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;