Reputation: 1054
I have two 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 which refers to databaseFactory
protected StankinQuestionnaireEntities DataContext
{
get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
}
I use Autofac and regiser DatabaseFactory
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerRequest();
on my repository i trying get data from navigation property in two ways
first line works fine(CalculationType contains one element)
but second line return null on property CalculationType
Why?
UPDATE I found that if remove the line ".InstancePerRequest()", everything works. But I do not fit this.
UPDATE2 for some reason, ef not created Proxy class
Upvotes: 4
Views: 1236
Reputation: 51
I had same issue, for fist time when page is loaded it works fine, when i refresh the page i get null reference exception. in my case i was using Task
System.Threading.Tasks.Task.Run(() =>
{
});
this task use Dbcontext for insert one record. I just remove Task and everything start working fine.
Upvotes: 0
Reputation: 368
You definitely have different values of ProxyCreationEnabled property for your database contexts.
If you look at the types of the picked entities in your screenshots, you can see that the first one has type System.Data.Entity.DynamicProxies.Indicator_E... and the second one has type StankinQuestionnaire.Model.Indicator.
That means that ProxyCreationEnabled is true for the first database context and the property is false for the second one. So, lazy loading does not work in the second case.
Try to search where ProxyCreationEnabled is set in your project, probably you have more than one place for that.
Upvotes: 1
Reputation: 485
Try this:
DbContext.Configuration.ProxyCreationEnabled = true;
DbContext.Configuration.LazyLoadingEnabled = true;
If DbContext.Configuration.ProxyCreationEnabled is set to false, DbContext will not load child objects for some parent object unless Include method is called on parent object. Setting DbContext.Configuration.LazyLoadingEnabled to true or false will have no impact on its behaviours.
If DbContext.Configuration.ProxyCreationEnabled is set to true, child objects will be loaded automatically, and DbContext.Configuration.LazyLoadingEnabled value will control when child objects are loaded.
I think this post is the same of this: Why EF navigation property return null?
Upvotes: 0