Entity Framework 6:禁用延迟加载并专门加载包含的表
Entity Framework 6: Disable Lazy Loading and specifically load included tables
我们当前的系统默认使用延迟加载(这是我将要禁用的功能,但现在无法完成)
对于这个基本查询,我想要 return 两个 table,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 中的数据 table
链接的实体Customer和Note都是null,我这里做错了什么?
我用下面的方法得到了它,这比我在其他地方找到的要简单得多
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 table,相关的注释和注释中的相关用户。
这就是你要实现的eager loading。
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();
我们当前的系统默认使用延迟加载(这是我将要禁用的功能,但现在无法完成)
对于这个基本查询,我想要 return 两个 table,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 中的数据 table
链接的实体Customer和Note都是null,我这里做错了什么?
我用下面的方法得到了它,这比我在其他地方找到的要简单得多
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 table,相关的注释和注释中的相关用户。
这就是你要实现的eager loading。
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();