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

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

问题描述

出于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记录作为列表嵌套在事件记录内的位置。

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

我可以在声明中进行大范围的延迟加载或弄清楚用于指定延迟加载的新格式时使用一些帮助。

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

Caz

推荐答案

因此,似乎EF Core当前不支持延迟加载。它的到来,但可能要过一段时间。

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

现在,如果还有其他人遇到此问题并正在苦苦挣扎。以下是使用急切加载的演示,这是您现在必须使用的内容。

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.

在拥有人物对象和那个对象之前说

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!!!

Caz

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

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