实体框架4负载,包括结合 [英] Entity Framework 4 load and include combine
问题描述
如果我有3个表:
表1,表2,表3
和表3中有一个FK表2,其中有一个FK以表1
然后我打开我的对象是这样的:
使用(实体实体=新的实体()
{
表1表= entities.Table1.FirstOrDefault();
table.Table2.Load();
}
我怎样才能急切地负载表3至表2,因为惰性加载被关闭。
我知道我可以使用在FirstOrDefault声明包括,但它会产生太大了加盟。
答案
使用(实体实体=新的实体())
{
表1表=实体.Table1.FirstOrDefault();
无功表2 = table.Table2.CreateSourceQuery()包括(表3)
.Execute(MergeOption.AppendOnly);
table.Table2.Attach(表2);
}
我怎样才能急切地负载表3至表2,因为惰性加载是
关掉
块引用>
您可以试试:
使用(实体实体=新的实体())
{
表1表= entities.Table1.FirstOrDefault ();
table.Table2.CreateSourceQuery()包括(表3)
.Execute(MergeOption.AppendOnly)。
}
我假设你使用的是源于实体
EntityObject
,不波苏斯,即table.Table2
是EntityCollection< T>
或的EntityReference< T>
。我不是100%肯定,如果如预期上面的代码将工作。If I have 3 tables:
Table1, Table2, Table3
And Table3 has a FK for Table2, which has a FK to Table1
Then I load my object like this:
using(Entities entities = new Entities() { Table1 table = entities.Table1.FirstOrDefault(); table.Table2.Load(); }
How can I eagerly load table3 to table2 because LazyLoading is switched off.
I know I can use Include in the FirstOrDefault statement, but it will generate a much too big join.
ANSWER
using(Entities entities = new Entities()) { Table1 table = entities.Table1.FirstOrDefault(); var table2 = table.Table2.CreateSourceQuery().Include("Table3") .Execute(MergeOption.AppendOnly); table.Table2.Attach(table2); }
解决方案How can I eagerly load table3 to table2 because LazyLoading is switched off.
You can try:
using(Entities entities = new Entities()) { Table1 table = entities.Table1.FirstOrDefault(); table.Table2.CreateSourceQuery().Include("Table3") .Execute(MergeOption.AppendOnly); }
I assume that you are using entities derived from
EntityObject
, not POCOs, i.e.table.Table2
is anEntityCollection<T>
orEntityReference<T>
. I am not 100% sure if the above code will work as expected.这篇关于实体框架4负载,包括结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!