5

I have seen the [DebuggerNonUserCode] and [ExcludeFromCodeCoverage] attributes in resources and other SO questions about exlcuding code from coverage statistics, and wanted to know if it was possible to automatically add this attribute to the classes in the code generated by the Entity Framework using .NET 4.0.

Also would it need to be class level or could it be on the diagram.Designer.cs level, needing one attribute for all code generated by that diagram?

Community
  • 1
  • 1
StuperUser
  • 10,555
  • 13
  • 78
  • 137
  • Yes, it's possible. [You can use a custom T4 template to control the codegen.](http://blogs.msdn.com/b/efdesign/archive/2009/01/22/customizing-entity-classes-with-t4.aspx) It's easier than you might think. – Craig Stuntz Jan 07 '11 at 17:09
  • A consideration is that partial classes (which Entity Framework creates) merge attributes, so if extended functionality in other partial classes is to be included, it will have to be method level. – StuperUser Jan 07 '11 at 17:39

1 Answers1

2

Since partial classes (which Entity Framework creates) merge attributes, extended functionality in other partial classes are also excluded if the attribute is class level in the template, it will have to be applied at the method level.

The best way that I've found to do this is using T4 (as recommended in @Craig Stuntz's answer) to:

  • include: using System.Diagnostics.CodeAnalysis; at the top of the file

Then apply [ExcludeFromCodeCoverage] to getters, setters and Factory methods by searching for:

  • #>get
  • #>set
  • Template_FactoryMethodComment

and placing them in the appropriate place.

This was made a lot easier using Tangible's T4 editor Extension for VS.

This is my first attempt and it seems to work, "your milage may vary", so complete a test run to make sure everything's working as necessary.

Community
  • 1
  • 1
StuperUser
  • 10,555
  • 13
  • 78
  • 137