Why does correct navigation sometimes return null?

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 a factory database

public class DatabaseFactory
{
    private StankinQuestionnaireEntities dataContext;
    public StankinQuestionnaireEntities Get()
    {
        return dataContext ?? (dataContext = new StankinQuestionnaireEntities());
    }
}

and a property that refers to databaseFactory

protected StankinQuestionnaireEntities DataContext
{
    get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
}

I am using Autofac and regizer DatabaseFactory

builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerRequest();

in my repository I try to get data from the navigation property in two ways

enter image description here

first line works fine (CalculationType contains one element)

enter image description here

but the second line returns null in the CalculationType property

enter image description here

Why?

UPDATE I found that if you delete the line ".InstancePerRequest ()", everything will work. But that doesn't suit me.

UPDATE2 for any reason, ef proxy class not created

+4
2

ProxyCreationEnabled .

, , System.Data.Entity.DynamicProxies.Indicator_E... StankinQuestionnaire.Model.Indicator.

, ProxyCreationEnabled , . , .

, ProxyCreationEnabled , , .

+1

:

DbContext.Configuration.ProxyCreationEnabled = true;    
DbContext.Configuration.LazyLoadingEnabled = true;  

DbContext.Configuration.ProxyCreationEnabled false, DbContext - , Include. DbContext.Configuration.LazyLoadingEnabled true false .

DbContext.Configuration.ProxyCreationEnabled true, , DbContext.Configuration.LazyLoadingEnabled .

, : EF null?

0

All Articles