【发布时间】:2016-10-10 14:27:54
【问题描述】:
我在 EF4 中使用查询来拉回记录并根据其中的数据通过各种其他方式(不是 EF)处理信息,因此我经常在列表中分离 EF 对象。
在这种情况下,我在 EntityFramework 4.0 中有一个未加载相关实体的查询,即使我使用的是 .Include("...") 方法。
using (MyDBEntities ctx = new MyDBEntities())
{
ctx.ContextOptions.LazyLoadingEnabled = false;
// Get the first X records that need to be processed
var q = (from t in ctx.DBTables
.Include("Customer")
let c = t.Customer
where t.statusID == (int)Enums.Status.PostProcessing
&& c.isActive == true
select t
).Take(batchSize).ToList();
foreach (DBTable t in q)
{
// this results in c == null
Customer c = t.Customer;
// However t.CustomerID has a value, thus I know
// that t links to a real Customer record
Console.WriteLine(t.CustomerID);
}
}
任何人都可以帮助我理解为什么客户没有加载,即使我明确声明要包含它?
【问题讨论】:
-
什么是
batchSize?当您单步执行foreach循环时,您能看到您有多少条记录并且您能看到任何值吗? -
batchSize 是一个 int,它会更早地传递给函数。
标签: c# entity-framework entity-framework-4