1

Using Visual Studio 2013 in an MVC 5 project with Entity Framework 6, I used "ADO Entity Data Model" (Code First from Database) to add some models to my project. In this case, the tables have relationships.

When the Model wizard was done adding the models & context to my application, I saw these warning attributes on the constructors of the secondary tables. Here is one example.

My question is whether there is some way I can refactor the code so that the warning is no longer there, or is this simply something I learn to ignore because of the way that Entity Framework understands database-first SQL Server relationships?

public partial class StudentList
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", 
            "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public StudentList()
    {
        CreditSlipLogs = new HashSet<CreditSlipLog>();
    }

    // ... code

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",
            "CA2227:CollectionPropertiesShouldBeReadOnly")]

    public virtual ICollection<CreditSlipLog> CreditSlipLogs { get; set; }

    }
}

I looked on MSDN for guidance on CA2214 and CA2227. While no doubt accurate, it wasn't helpful, because I didn't see any instruction on how to resolve this when it was created by the Data Model wizard.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • 1
    You are getting this attribute added because you are initializing a navigation property that refers to an Entity. This entity may not have been initialized yet. You are then setting it to a new empty object. See Gert Arnold's answer to this SO question: http://stackoverflow.com/questions/20757594/ef-codefirst-should-i-initialize-navigation-properties – ghg565 Jun 23 '16 at 20:57
  • Oops...I misread that – ghg565 Jun 23 '16 at 21:04
  • I read the link. I'm only getting more confused. –  Jun 23 '16 at 21:06
  • Here is a better explanation:if someone inherits from your class, and overrides a method or property that is accessed in your constructor - then the overriding implementation will be hit before the constructor for the inherited class has run. This might lead to problems if the overriding implementation relies on state set in the constructor. To satisfy the warning, you need to make the properties and methods accessed in the constructor non-virtual (you could make the type sealed, if appropiate).-http://stackoverflow.com/questions/3963219/code-analysis-warning-2214-how-best-to-fix driis – ghg565 Jun 23 '16 at 21:23
  • As a test, I made the class sealed and struck out the `virtual` from the ICollection property. In my modelBuilder (in the Context class), I am unable to join with the related table. ""Inaccessible due to protection level." –  Jun 23 '16 at 21:31

1 Answers1

2

Those aren't warnings, they're attributes added to the code to prevent build warnings. Details for SuppressMessageAttribute.

The generated code would trigger these warnings because of the way EF handles lazy-loading, so they added this attribute to the class template so you wouldn't receive build warnings.

Trey Mack
  • 4,215
  • 2
  • 25
  • 31
  • Some more details from another post : https://stackoverflow.com/questions/438803/explain-system-diagnostics-codeanalysis-suppressmessage – marifrahman Apr 08 '19 at 05:23