C# 实体框架6延迟加载问题
我有这个实体:C# 实体框架6延迟加载问题,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我有这个实体: public class Crash : BaseModel { public string Message { get; set; } public DateTime Time { get; set; } public int ApplicationId { get; set; } public virtual Application Application { get; set; } } 以下功能非常完善,导航属性应用程序正确无误: _co
public class Crash : BaseModel
{
public string Message { get; set; }
public DateTime Time { get; set; }
public int ApplicationId { get; set; }
public virtual Application Application { get; set; }
}
以下功能非常完善,导航属性应用程序正确无误:
_context.Set<T>().ToList();
虽然在获取的项目属性应用程序中
为null
,但检索到了正确的项目
我应该怎么做才能找到带有表达式的项,并且仍然自动加载导航属性???First()只返回一个值,而不是一个集合。不像托利斯()那样
您的控件将等待集合的到来
单值不是只有单值的集合
您需要使用First()返回值的一条记录创建一个集合。
这将是一个包含一条记录的列表。您需要通过将其包含在查询中来加载它。这就是所谓的急加载
_context.Set<Crash>().Include(x => x.Application).First(expression);
\u context.Set().Include(x=>x.Application).First(表达式);
您还可以关闭延迟加载,并且在访问导航属性时将加载该属性。但请记住,这可能会导致性能问题。关闭延迟加载将产生全局效果
如果您只希望加载此特定属性而不包含我上面所示的include,那么可以将该属性设置为非虚拟属性
是有关延迟加载等更多信息的链接
_context.Set<Crash>().Include(x => x.Application).First(expression);