kri*_*per 10 c# asp.net asp.net-mvc entity-framework
我有两个型号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; }
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
我执行这段代码
var indicator = DataContext.Indicators.FirstOrDefault(i => i.ID == indicatorID);
var test = DataContext.CalculationTypes.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
第一行在导航属性CalculationTypes上返回null

第二行返回空集合.
为什么?
更新
快照数据库

项目链接https://github.com/wkololo4ever/Stankin
添加计算
public class ?alculation
{
public long ID { get; set; }
public virtual CalculationType CalculationType { get; set; }
public virtual ApplicationUser Creator { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
1) 是否启用了延迟加载?如果没有,您需要使用“.Include”语法显式加载导航属性。
2)您确定EF应该能够检测到这种关系吗?您使用的是代码优先还是数据库优先?
编辑:3) 您确定您的数据库中有数据并且从 Indicator 到 IndicatorGroup 的外键具有该特定记录的值吗?我这么说是因为如果根本没有数据,值“null”是有效的。
PS 如果您在 Indicator 上没有看到名为“IndicatorGroupId”的外键,则表“IndicatorGroup”上可能有一个“IndicatorId”,在这种情况下 - 根据您提供的名称 - 您的数据库配置错误,您需要使用流畅的语法或数据属性来指导 EF 如何制作外键。
行为相同,但根本原因与所选答案不同:
如果关闭,导航属性也可以为 null myContext.Configuration.AutoDetectChangesEnabled
非常明显,但是当我实施一些性能改进时,这让我感到困惑。
尝试这个:
DbContext.Configuration.ProxyCreationEnabled = true;
DbContext.Configuration.LazyLoadingEnabled = true;
Run Code Online (Sandbox Code Playgroud)
如果 DbContext.Configuration.ProxyCreationEnabled 设置为 false,除非在父对象上调用 Include 方法,否则 DbContext 不会为某些父对象加载子对象。将 DbContext.Configuration.LazyLoadingEnabled 设置为 true 或 false 不会对其行为产生影响。
如果 DbContext.Configuration.ProxyCreationEnabled 设置为 true,则将自动加载子对象,并且 DbContext.Configuration.LazyLoadingEnabled 值将控制何时加载子对象。
我认为这是问题:
编辑:3) 您确定您的数据库中有数据并且从 Indicator 到 IndicatorGroup 的外键具有该特定记录的值吗?我这么说是因为如果根本没有数据,值“null”是有效的。
PS 如果您在 Indicator 上没有看到名为“IndicatorGroupId”的外键,则表“IndicatorGroup”上可能有一个“IndicatorId”,在这种情况下 - 根据您提供的名称 - 您的数据库配置错误,您需要使用流畅的语法或数据属性来指导 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; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14814 次 |
| 最近记录: |