4

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

enter image description here

first line works fine(CalculationType contains one element)

enter image description here

but second line return null on property CalculationType

enter image description here

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

kriper
  • 1,034
  • 2
  • 12
  • 23
  • In the first case, you have a proxy (see its runtime type). In the second case, you have the base model type. The behavior of navigation properties that you expect works only on proxies, because proxies are subtypes of your base model type that override your navigation properties to provide easy and lazy loading. If there is no proxy, the framework doesn't get a chance to override the navigation property and give you the behavior you want. – Theodoros Chatzigiannakis Mar 28 '15 at 20:37
  • @TheodorosChatzigiannakis but why ef not created proxy class? ProxyEnable set true http://savepic.net/6498011.png – kriper Mar 29 '15 at 08:39

3 Answers3

1

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.

  • But yes for some reason, is not created Proxy class. – kriper Mar 29 '15 at 08:38
  • Well, I have downloaded your project. I get a link from your another question. Then I put the code for quering an indicator by Id in `UpdateIndicator` method of `CalculationTypeRepository` and called it from `Home` controller. No issues. Proxy is created for both objects. [Screenshot](http://savepic.net/6489835.png). So, I still think that you disable proxy creation somewhere in your fresh code. – Viktar Tserashchuk Mar 29 '15 at 10:22
  • Thank you very much! Your attempt to help me find the problem. I called method that attaches to the null model property http://savepic.net/6530791.png – kriper Mar 29 '15 at 11:11
0

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?

Community
  • 1
  • 1
toannm
  • 485
  • 4
  • 9
  • I asked this http://stackoverflow.com/questions/29272581/why-ef-navigation-property-return-null/29272906#29272906 :) I found that if remove the line ".InstancePerRequest()", everything works. But I do not fit this. – kriper Mar 28 '15 at 16:48
  • @kriper Looks like the context is disposed while you're in debug view. Maybe you can override the context's `Dispose` method and set a breakpoint in in. – Gert Arnold Mar 28 '15 at 17:55
  • @GertArnold in dispose, navigation property null too http://savepic.net/6536837.png – kriper Mar 28 '15 at 18:09
  • @GertArnold The most interesting point that some navigation properties works fine, but that property (CalculationTypes) binds only after manual loading or manual creation of context. – kriper Mar 28 '15 at 18:21
0

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.

Majid Omar
  • 51
  • 4