The database first automatically calls the CA2214 class: do not call overriden 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 CA2214: do not call overriden methods in constructors, but since this is auto-generated code, I think I should not change it, how should I approach this scenario? thanks in advance

+6
source share
3 answers

The code you posted does not cause an error, it just violates the rule of code analysis and will work fine. The generated code is also correct in design to provide lazy loading. You must suppress this warning for these files.

+5
source

In this case, you can ignore this warning, but if you want to avoid it, you can manually create a personal field for each virtual object (and setter / getter manually). Then in the constructor, specify a personal field, not a property. Thus, you never run the virtual property installer inside the constructor, which is the reason for the warning.

+1
source

You can turn off warnings in the generated code in Visual Studio projects using the checkbox on the code analysis properties page:

The Suppress Results from Generated Code check box on the Code Analysis project properties page allows you to choose whether you want to see Code Analysis warnings from code generated by a third-party tool. - How: Suppress code analysis warnings for generated code

0
source

Source: https://habr.com/ru/post/972694/


All Articles