Why does the EF navigation property return null?

I have two models 1)

public class Indicator
{
    public long ID { 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 virtual IList<TestEntity> TestEntitys { get; set; }
    public virtual IndicatorGroup IndicatorGroup { get; set; }
}

2)

public class CalculationType
{
    public long ID { 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; }
    public virtual IList<alculation> Calculations { get; set; }
}

I execute this code

var indicator = DataContext.Indicators.FirstOrDefault(i => i.ID == indicatorID);
var test = DataContext.CalculationTypes.FirstOrDefault();

first line returns null for the CalculationTypes navigation property enter image description here

The second line returns an empty collection. enter image description hereWhy?

UPDATE snapshot database enter image description hereenter image description here project link https://github.com/wkololo4ever/Stankin

calculation added

    public class alculation
{
    public long ID { get; set; }

    public virtual CalculationType CalculationType { get; set; }
    public virtual ApplicationUser Creator { get; set; }
}
+7
source share
4 answers

1) Is lazy loading enabled? If not, you need to explicitly load your navigation properties using the ".Include" syntax.

2) , EF ? Code First Database?

: 3) , Indicator to IndicatorGroup ? , "null" , .

P.S. "IndicatorGroupId", "IndicatorGroup" "IndicatorId", - , , - , EF , .

+2

:

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 .

, :

: 3) , , Indicator to IndicatorGroup ? , "null" , .

P.S. "IndicatorGroupId", "IndicatorId" "IndicatorGroup", - , - , EF , .

, .

public class CalculationType
{
    public long ID { get; set; }
    public string UnitName { get; set; }
    public int Point { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateChanged { get; set; }
    [ForeignKey("IndicatorID")]
    public string IndicatorId { get; set; } //this is the foreign key, i saw in your database is: Indicator_ID, avoid this, rename it to IndicatorID or IndicatorId

    public virtual Indicator Indicator { get; set; }
    public virtual IList<alculation> Calculations { get; set; }
}
+2

, , :

, myContext.Configuration.AutoDetectChangesEnabled

, , .

+1

: . , null .

, . "include" IQuearable .

: , , "" IQueryable.

+1

All Articles