实体框架核心 - 延迟加载 [英] Entity Framework Core - Lazy Loading

查看:31
本文介绍了实体框架核心 - 延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

屈服于我的 Visual Studios 请求,我使用 Entity Framework Core (1.0.1) 开始了我的最新项目

Bowing to my Visual Studios request, I started my latest project using Entity Framework Core (1.0.1)

所以编写我的数据库模型,因为我总是使用虚拟"说明符来启用列表的延迟加载.虽然在加载父表时,子列表似​​乎永远不会加载.

So writing my database models as I always have using the 'virtual' specifier to enable lazy loading for a List. Though when loading the parent table it appears that the child list never loads.

父模型

public class Events
{
    [Key]

    public int EventID { get; set; }
    public string EventName { get; set; }
    public virtual List<EventInclusions> EventInclusions { get; set; }
}

子模型

public class EventInclusions
{
    [Key]
    public int EventIncSubID { get; set; }
    public string InclusionName { get; set; }
    public string InclusionDesc { get; set; }
    public Boolean InclusionActive { get; set; }

}

向这些表添加新记录似乎很有效,因为我已经习惯了将 EventInclusions 记录作为列表嵌套在 Events 记录中的位置.

Adding new records to these tables seems to work as I am used to where I can nest the EventInclusions records as a List inside the Events record.

虽然当我查询这个表时

_context.Events.Where(e => e.EventName == "Test")

问题

EventInclusions 将返回空值,而不管幕后的数据如何.

EventInclusions will return a null value regardless of the data behind the scenes.

阅读了一些之后,我感觉这是我通常使用的 EF6 和 EF Core 之间的变化

After reading a bit I am getting the feeling this is a change between EF6 which I normally use and EF Core

我可以使用一些帮助来制作全面的 Lazy Loading on 语句或找出用于指定 Lazy Loading 的新格式.

I could use some help in either making a blanket Lazy Loading on statement or figuring out the new format for specifying Lazy Loading.

卡兹

推荐答案

所以看来 EF Core 目前不支持延迟加载.它的到来,但可能需要一段时间.

So it appears that EF Core does not currently support lazy loading. Its coming but may be a while off.

现在,如果其他人遇到这个问题并且正在苦苦挣扎.下面是使用 Eager loading 的演示,这是您现在必须使用的.

For now if anyone else comes across this problem and is struggling. Below is a demo of using Eager loading which is what for now you have to use.

在你有一个 person 对象之前说,该对象在另一个表中包含一个帽子列表.

Say before you had a person object and that object contained a List of Hats in another table.

而不是写作

var person = _context.Person.Where(p=> p.id == id).ToList();

person.Hats.Where(h=> h.id == hat).ToList();

你需要写

var person = _context.Person.Include(p=> p.Hats).Where(p=> p.id == id).ToList();

然后 person.Hats.Where(h=> h.id == hat).ToList(); 将起作用

如果您有多个列表 - 链接包含

var person = _context.Person.Include(p=> p.Hats).Include(p=> p.Tickets)
                            .Include(p=> p.Smiles).Where(p=> p.id == id).ToList();

我有点明白为什么这种方法更安全,因为您不必加载可能会减慢速度的庞大数据集.但我希望他们能尽快恢复延迟加载!!!

I kinda get why this method is safer, that your not loading huge data sets that could slow things down. But I hope they get Lazy loading back soon!!!

卡兹

这篇关于实体框架核心 - 延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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