2

I am working on a asp.net core 2.2 web api project. I have a separate class library called MyProject.Db that I have scaffolded the db context from an existing database. The MyProjDbContext contains an auto-generated method called

protected override void OnModelCreating(ModelBuilder modelBuilder) 

After some coding I had to perform some code analysis, so in visual studio community 2019 I ran the code metrics from the menu Analyze --> Calculate Code Metrics --> For MyProject.Db.

The code metrics results showed a very low maintainability index for the particular method and lowered the general percentage.

The low maintainability index

Since it is auto-generated code I want to leave it out of the analysis.

I have read some of microsoft's documentation regarding a customized configuration, such as here and here but these refer mostly to unit tests and they did not help me much.

I also tried 2 solutions presented here but none helped either.

  1. I tried using the [ExcludeFromCodeCoverage] attribute but it did not have any effect.
  2. I created a custom [ExcludeFromCodeCoverage] attribute and I added it in the runsettings file. But I got confused as to where I will use it via the Unit tests.

EDIT

According to this way here, even though it seems kind of old, if we add the GeneratedCode attribute in the class/method, it would exclude the code from the code metrics. So the format is as follows:

        [GeneratedCode("Microsoft.EntityFrameworkCore.Tools", "2.2.4")]        
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
                //Scaffolded models here
        }

As it turned out this is another failed attempt. The method was not ommited in the code metrics.

Is there a way to exclude a method/class from a class library for this type of code analysis?

SoftDev30_15
  • 463
  • 7
  • 20

0 Answers0