Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database first automatic class causes CA2214: Do not call overridable methods in constructors

Auto generated class by EntityFramework 6.1:

public partial class STUDENT
{
    public STUDENT()
    {
        this.STUDENT_GROUPS = new HashSet<STUDENT_GROUPS>();
    }
    public int ID { get; set; }
    public int PERSON { get; set; }
    ...
    public virtual ICollection<STUDENT_GROUPS> STUDENT_GROUPS { get; set; }
    ...
}

And STUDENT_GROUPS:

public partial class STUDENT_GROUPS
{
    public int ID { get; set; }
    public int GROUPS_GRP { get; set; }
    public int STUDENT { get; set; }

    public virtual STUDENT STUDENT1 { get; set; }
    public virtual GROUPS_GRP GROUPS_GRP1 { get; set; }
}

Throws a CA2214: Do not call overridable methods in constructors, but since this is an autogenerated code, I think I am not supposed to change it, how should i approach this scenario? Thanks in advance

like image 356
Juanito Avatar asked Jul 23 '14 10:07

Juanito


1 Answers

The code you have posted is not throwing an error, it is just violating a code analysis rule and will work fine. The generated code is correct and by design to allow for lazy loading. You should suppress that warning for those files.

like image 188
DavidG Avatar answered Nov 10 '22 09:11

DavidG