【问题标题】:Entity Framework 6: Disable Lazy Loading and specifically load included tables实体框架 6:禁用延迟加载并专门加载包含的表
【发布时间】:2018-03-15 06:20:01
【问题描述】:

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

对于这个基本查询,我想返回两个表,CustomerNote 和 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 表中的数据

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

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

            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 的相关用户。

【问题讨论】:

    标签: entity-framework entity-framework-6 lazy-loading


    【解决方案1】:

    这就是你想要实现的所谓的急切加载。

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

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

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

    【讨论】:

    • 谢谢我在搞砸的时候发现了这一点,你的首要任务基本上就是我所使用的,如果我包含 LazyLoadingEnabled =false,它确实有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    相关资源
    最近更新 更多