实体框架(v6)渴望加载的怪异行为-'Gotcha'

时间:2018-11-15 05:54:04

标签: entity-framework-6 eager-loading navigation-properties

以下两个查询是否等效? (请注意.include的位置)

==(V1)

using (var ctx = new Entities()) {

    ctx.Configuration.ProxyCreationEnabled = false; //return stronglyTyped Entity, not dynamic entity...

    IQueryable<TB_MyHeader> query = from hd in ctx.TB_MyHeader.Include(h => h.TB_MyLines)
        join wo in ctx.TB_AnotherTable on hd.fkId equals wo.ID
        where wo.woPk == @id
    orderby hd.PoItem
    select hd;

    var headerPlusLines = query
        .AsNoTracking() 
        .ToList();

    return headerPlusLines;
}

==(V2)

using (var ctx = new Entities()) {

    ctx.Configuration.ProxyCreationEnabled = false; //return stronglyTyped Entity, not dynamic entity...

    IQueryable<TB_MyHeader> query = (from hd in ctx.TB_MyHeader
        join wo in ctx.TB_AnotherTable on hd.fkId equals wo.ID
    where wo.woPk == @id
    orderby hd.PoItem
    select hd)
    .Include(h => h.TB_MyLines);


    var headerPlusLines = query
        .AsNoTracking() 
        .ToList();

    return headerPlusLines;
}

第一个版本(V1)可能无法将子代获取到Nav属性中,具体取决于如何将ProxyCreationEnabled,LazyLoadingEnabled设置为true / false。

实际上,与预期结果相反;如果我将LazyLoadingEnabled设置为false,则根本不会加载任何子级; 如果我希望它“不要偷懒,请立即加载!”

怎么回事?

0 个答案:

没有答案