实体框架6:禁用延迟加载,特别是加载包含的表 [英] Entity Framework 6: Disable Lazy Loading and specifically load included tables
问题描述
默认情况下,我们当前的系统使用的是延迟加载(这是我要禁用的功能,但目前无法完成)
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表,相关便笺和相关用户.
This returns my CustomerNote table, related Notes and related Users from the Notes.
推荐答案
这就是您想要实现的渴望加载.
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屋!