c# - Why navigation propery sometimes return null? -


i have 2 models

public class indicator {     public long indicatorid { get; set; }     public string name { get; set; }     public int maxpoint { get; set; }     public string comment { get; set; }     public datetime datechanged { get; set; }     public datetime datecreated { get; set; }      public virtual ilist<calculationtype> calculationtypes { get; set; } }  public class calculationtype {     public long calculationtypeid { get; set; }     public string unitname { get; set; }     public int point { get; set; }     public datetime datecreated { get; set; }     public datetime datechanged { get; set; }      public virtual indicator indicator { get; set; } } 

i have database factory

public class databasefactory {     private stankinquestionnaireentities datacontext;     public stankinquestionnaireentities get()     {         return datacontext ?? (datacontext = new stankinquestionnaireentities());     } } 

and property refers databasefactory

protected stankinquestionnaireentities datacontext {     { return datacontext ?? (datacontext = databasefactory.get()); } } 

i use autofac , regiser databasefactory

builder.registertype<databasefactory>().as<idatabasefactory>().instanceperrequest(); 

on repository trying data navigation property in 2 ways

enter image description here

first line works fine(calculationtype contains 1 element)

enter image description here

but second line return null on property calculationtype

enter image description here

why?

update found if remove line ".instanceperrequest()", works. not fit this.

update2 reason, ef not created proxy class

you have different values of proxycreationenabled property database contexts.

if @ types of picked entities in screenshots, can see first 1 has type system.data.entity.dynamicproxies.indicator_e... , second 1 has type stankinquestionnaire.model.indicator.

that means proxycreationenabled true first database context , property false second one. so, lazy loading not work in second case.

try search proxycreationenabled set in project, have more 1 place that.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -