实体框架 6:禁用延迟加载并专门加载包含的表 [英] Entity Framework 6: Disable Lazy Loading and specifically load included tables

查看:17
本文介绍了实体框架 6:禁用延迟加载并专门加载包含的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前的系统默认使用延迟加载(我将禁用它,但现在无法完成)

Our current system is using Lazyloading by default (it is something I am going to be disabling but it can't be done right now)

对于这个基本查询,我想返回两个表,CustomerNote 和 Note.

For this basic query I want to return two tables, CustomerNote and Note.

这是我的查询

            using (var newContext = new Entities(true))
            {
                newContext.Configuration.LazyLoadingEnabled = false;


                var result = from customerNotes in newContext.CustomerNotes.Include(d=>d.Note)
                             join note in newContext.Notes
                                on customerNotes.NoteId equals note.Id
                             where customerNotes.CustomerId == customerId
                             select customerNotes;

                return result.ToList();
            }

但我的结果只包含 CustomerNote 表中的数据

My result however only contains the data in the CustomerNote table

链接的实体 Customer 和 Note 都是空的,我在这里做错了什么?

The linked entities Customer and Note are both null, what am I doing wrong here?

我得到了它,这比我在其他地方找到的要简单得多

I got it working with the following which is much simpler than what I've found elsewhere

            Context.Configuration.LazyLoadingEnabled = false;
            var result = Context.CustomerNotes.Where<CustomerNote>(d => d.CustomerId == customerId)
                .Include(d=>d.Note)
                .Include(d=>d.Note.User);
            return result.ToList();

这将返回我的 CustomerNote 表、相关的 Notes 以及来自 Notes 的相关用户.

This returns my CustomerNote table, related Notes and related Users from the Notes.

推荐答案

也就是你想要实现的叫做eager loading.

That is callled eager loading you want to achieve.

var customerNotes = newContext.CustomerNotes.Include(t=> t.Node).ToList();

这应该可以,我不太了解关键字语法.如果上面的代码不起作用,试试这个:

This should work, i don't really understand the keyword syntax. If the code above doesn't work try this:

        var customerNotes = newContext.CustomerNotes.Include(t=> t.Node).Select(t=> new {
        Node = t.Node,
        Item = t
    }).ToList();

这篇关于实体框架 6:禁用延迟加载并专门加载包含的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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