为什么EF导航属性返回NULL? [英] Why EF navigation property return null?

查看:1772
本文介绍了为什么EF导航属性返回NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型
1)

 公共类指标
{
    众长ID {搞定;组; }
    公共字符串名称{;组; }
    公众诠释MaxPoint {搞定;组; }
    公共字符串评论{搞定;组; }
    公众的DateTime DateChanged {搞定;组; }
    公众的DateTime dateCreated会获得{;组; }    公共虚拟的IList< CalculationType> CalculationTypes {搞定;组; }
    公共虚拟的IList< TestEntity> TestEntitys {搞定;组; }
    公共虚拟IndicatorGroup IndicatorGroup {搞定;组; }
}

2)

 公共类CalculationType
{
    众长ID {搞定;组; }
    公共字符串单位名{获得;组; }
    公众诠释点{搞定;组; }
    公众的DateTime dateCreated会获得{;组; }
    公众的DateTime DateChanged {搞定;组; }    公共虚拟指示灯指示灯{搞定;组; }
    公共虚拟的IList<Сalculation>计算{搞定;组; }
}

我执行此code

  VAR指标= DataContext.Indicators.FirstOrDefault(I => i.ID == indicatorID);
变种测试= DataContext.CalculationTypes.FirstOrDefault();

在导航属性CalculationTypes第一线返回NULL

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

更新
快照数据库
在这里输入的形象描述
项目链接 https://github.com/wkololo4ever/Stankin

添加计算

 公共类Сalculation
{
    众长ID {搞定;组; }    公共虚拟CalculationType CalculationType {搞定;组; }
    公共虚拟ApplicationUser造物主{搞定;组; }
}


解决方案

1)已启用延迟加载?如果没有,你需要用.INCLUDE语法显式地加载您的导航属性。

2)你确定EF应该能够检测到的关系?你使用code首先或数据库第一?

编辑:3)你确定有数据在数据库中,并从指标到IndicatorGroup外键具有特定记录的价值?我这样说,因为值空是有效的,如果是根本不存在的数据。

P.S。如果您没有看到指示灯名为IndicatorGroupId外键,有可能是在桌子上IndicatorGroup一个IndicatorId,在这种情况下 - 从您提供的名称去 - 你的数据库是配置错误,你将需要使用流利的语法或数据属性来指导如何使外键EF。

I have two model 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 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

Second line return empty collection. Why?

UPDATE snapshot database project link 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; }
}

解决方案

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

2) Are you sure EF should be able to detect that relation? Did you use Code First or 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.

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.

这篇关于为什么EF导航属性返回NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆