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
first line works fine(calculationtype contains 1 element)
but second line return null on property calculationtype
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
Post a Comment