Explicit Initialization rules are conflicting with Entity Framework

amvankeulenamvankeulen Posts: 3

When trying to do code analysis on web services build using EF Core, I am constantly getting this violation

For this code:

This violation occurs because the DbSet object is build by the DBContext, which does not use explicit initialization

This error is occurring every time I'm accessing a database, which is leading to lots and lots of these violations.

I don’t think suppressing is my best bet, since the use of dbcontext is pretty integral to EF Core, though I'm not sure what a better solution would be

Any ideas on what to do with this?

Comments

  • Could you provide the violation with full path? Could you provide code sample showing violation? What I am most interested in is how this _dbContext variable is initialized and where,

  • amvankeulenamvankeulen Posts: 3

    Two examples:

    It doesn't look like it's complaining about _dbContext so much as it's complaining about the "Request" and "Check" properties. Both of those don't have explicit initializations, but that's by design. This is what the properties look like in the _dbContext class

    The dbContext is being initialized every time a new API call is made, which looks like this (_dbContext is just assigned this property):

Sign In or Register to comment.