为什么EF导航属性返回null?

时间:2022-08-30 11:50:03

I have two model 1)

我有两个型号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)

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 executing this code

我执行这段代码

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

first line return null on navigation property CalculationTypes 为什么EF导航属性返回null?

第一行在导航属性CalculationTypes上返回null

Second line return empty collection.为什么EF导航属性返回null? Why?

第二行返回空集合。为什么?

UPDATE snapshot database 为什么EF导航属性返回null?为什么EF导航属性返回null? project link https://github.com/wkololo4ever/Stankin

更新快照数据库项目链接https://github.com/wkololo4ever/Stankin

added Calculation

添加了计算

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

    public virtual CalculationType CalculationType { get; set; }
    public virtual ApplicationUser Creator { get; set; }
}

4 个解决方案

#1


4  

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.

如果将DbContext.Configuration.ProxyCreationEnabled设置为false,则除非在父对象上调用Include方法,否则DbContext将不会加载某些父对象的子对象。将DbContext.Configuration.LazyLoadingEnabled设置为true或false将不会对其行为产生任何影响。

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.

如果DbContext.Configuration.ProxyCreationEnabled设置为true,则将自动加载子对象,并且DbContext.Configuration.LazyLoadingEnabled值将控制何时加载子对象。

I think this is problem:

我认为这是个问题:

Edit: 3) Are you sure there is data in your database and that the foreign key from Indicator to IndicatorGroup has a value for that specific record? I am saying this because the value "null" is valid if there is simply no data.

编辑:3)您确定数据库中是否有数据,并且从Indicator到IndicatorGroup的外键具有该特定记录的值?我这样说是因为如果没有数据,值“null”是有效的。

P.S. If you do not see a foreign key on Indicator called "IndicatorGroupId", there might be an "IndicatorId" on the table "IndicatorGroup", in which case - going from the names you provided - your database is misconfigured and you will need to use fluent syntax or data attributes to instruct EF on how to make the foreign keys.

附:如果你没有在Indicator上看到名为“IndicatorGroupId”的外键,那么“IndicatorGroup”表上可能会有一个“IndicatorId”,在这种情况下 - 从你提供的名字开始 - 你的数据库配置错误,你需要使用流畅的语法或数据属性,以指示EF如何制作外键。

Try to this and make sure foreign key is corrected.

尝试这一点,并确保更正外键。

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


2  

1) Is Lazy Loading enabled? If not, you need to explicitly load your navigation properties with the '.Include' syntax.

1)是否启用了延迟加载?如果没有,则需要使用“.Include”语法显式加载导航属性。

2) Are you sure EF should be able to detect that relation? Did you use Code First or Database First?

2)你确定EF应该能够检测到这种关系吗?你使用Code First还是Database First?

Edit: 3) Are you sure there is data in your database and that the foreign key from Indicator to IndicatorGroup has a value for that specific record? I am saying this because the value "null" is valid if there is simply no data.

编辑:3)您确定数据库中是否有数据,并且从Indicator到IndicatorGroup的外键具有该特定记录的值?我这样说是因为如果没有数据,值“null”是有效的。

P.S. If you do not see a foreign key on Indicator called "IndicatorGroupId", there might be an "IndicatorId" on the table "IndicatorGroup", in which case - going from the names you provided - your database is misconfigured and you will need to use fluent syntax or data attributes to instruct EF on how to make the foreign keys.

附:如果你没有在Indicator上看到名为“IndicatorGroupId”的外键,那么“IndicatorGroup”表上可能会有一个“IndicatorId”,在这种情况下 - 从你提供的名字开始 - 你的数据库配置错误,你需要使用流畅的语法或数据属性,以指示EF如何制作外键。

#3


1  

Same behavior, but different root cause than selected answer:

相同的行为,但根本原因不同于选定的答案:

Navigation property can also be null if you turned off myContext.Configuration.AutoDetectChangesEnabled

如果关闭myContext.Configuration.AutoDetectChangesEnabled,导航属性也可以为null

Very obvious, but this got me when I was implementing some performance improvments.

非常明显,但是当我实施一些性能提升时,这就让我受益匪浅。

#4


1  

Check this out: Navigation Property With Code First . It mentions about why navigation property is null and the solutions of it.

看看这个:导航属性与代码优先。它提到了为什么导航属性为null及其解决方案。

By default, navigation properties are null, they are not loaded by default. For loading navigation property, we use “include” method of IQuearable and this type of loading is called Eager loading.

默认情况下,导航属性为null,默认情况下不加载它们。对于加载导航属性,我们使用IQuearable的“include”方法,这种类型的加载称为Eager加载。

Eager loading: It is a process by which a query for one type of entity loads the related entities as a part of query and it is achieved by “include” method of IQueryable.

渴望加载:这是一种过程,通过该过程,对一种类型的实体的查询将相关实体作为查询的一部分加载,并且通过IQueryable的“include”方法实现。

#1


4  

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.

如果将DbContext.Configuration.ProxyCreationEnabled设置为false,则除非在父对象上调用Include方法,否则DbContext将不会加载某些父对象的子对象。将DbContext.Configuration.LazyLoadingEnabled设置为true或false将不会对其行为产生任何影响。

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.

如果DbContext.Configuration.ProxyCreationEnabled设置为true,则将自动加载子对象,并且DbContext.Configuration.LazyLoadingEnabled值将控制何时加载子对象。

I think this is problem:

我认为这是个问题:

Edit: 3) Are you sure there is data in your database and that the foreign key from Indicator to IndicatorGroup has a value for that specific record? I am saying this because the value "null" is valid if there is simply no data.

编辑:3)您确定数据库中是否有数据,并且从Indicator到IndicatorGroup的外键具有该特定记录的值?我这样说是因为如果没有数据,值“null”是有效的。

P.S. If you do not see a foreign key on Indicator called "IndicatorGroupId", there might be an "IndicatorId" on the table "IndicatorGroup", in which case - going from the names you provided - your database is misconfigured and you will need to use fluent syntax or data attributes to instruct EF on how to make the foreign keys.

附:如果你没有在Indicator上看到名为“IndicatorGroupId”的外键,那么“IndicatorGroup”表上可能会有一个“IndicatorId”,在这种情况下 - 从你提供的名字开始 - 你的数据库配置错误,你需要使用流畅的语法或数据属性,以指示EF如何制作外键。

Try to this and make sure foreign key is corrected.

尝试这一点,并确保更正外键。

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


2  

1) Is Lazy Loading enabled? If not, you need to explicitly load your navigation properties with the '.Include' syntax.

1)是否启用了延迟加载?如果没有,则需要使用“.Include”语法显式加载导航属性。

2) Are you sure EF should be able to detect that relation? Did you use Code First or Database First?

2)你确定EF应该能够检测到这种关系吗?你使用Code First还是Database First?

Edit: 3) Are you sure there is data in your database and that the foreign key from Indicator to IndicatorGroup has a value for that specific record? I am saying this because the value "null" is valid if there is simply no data.

编辑:3)您确定数据库中是否有数据,并且从Indicator到IndicatorGroup的外键具有该特定记录的值?我这样说是因为如果没有数据,值“null”是有效的。

P.S. If you do not see a foreign key on Indicator called "IndicatorGroupId", there might be an "IndicatorId" on the table "IndicatorGroup", in which case - going from the names you provided - your database is misconfigured and you will need to use fluent syntax or data attributes to instruct EF on how to make the foreign keys.

附:如果你没有在Indicator上看到名为“IndicatorGroupId”的外键,那么“IndicatorGroup”表上可能会有一个“IndicatorId”,在这种情况下 - 从你提供的名字开始 - 你的数据库配置错误,你需要使用流畅的语法或数据属性,以指示EF如何制作外键。

#3


1  

Same behavior, but different root cause than selected answer:

相同的行为,但根本原因不同于选定的答案:

Navigation property can also be null if you turned off myContext.Configuration.AutoDetectChangesEnabled

如果关闭myContext.Configuration.AutoDetectChangesEnabled,导航属性也可以为null

Very obvious, but this got me when I was implementing some performance improvments.

非常明显,但是当我实施一些性能提升时,这就让我受益匪浅。

#4


1  

Check this out: Navigation Property With Code First . It mentions about why navigation property is null and the solutions of it.

看看这个:导航属性与代码优先。它提到了为什么导航属性为null及其解决方案。

By default, navigation properties are null, they are not loaded by default. For loading navigation property, we use “include” method of IQuearable and this type of loading is called Eager loading.

默认情况下,导航属性为null,默认情况下不加载它们。对于加载导航属性,我们使用IQuearable的“include”方法,这种类型的加载称为Eager加载。

Eager loading: It is a process by which a query for one type of entity loads the related entities as a part of query and it is achieved by “include” method of IQueryable.

渴望加载:这是一种过程,通过该过程,对一种类型的实体的查询将相关实体作为查询的一部分加载,并且通过IQueryable的“include”方法实现。