Partial eager loading child entity (load specific fileds)

若如初见. 提交于 2019-12-13 21:03:54

问题


I searched a bit and understands that I can use projection to partially load an entity , the question becomes is there a way to partially eager loading a child? Say I have the following

Entity A has

Id
Name
EntityB

and Entity B has

Id
StuffToBeLoaded1
StuffToBeLoaded2
OtherStuffNotToBeLoaded

How can I load A with B , and B only has stuffToBeLoaded1 and stuffToBeLoaded2? I guess I cannot call .Inlucde("EntityB") otherwise it is fully loaded, is it?


回答1:


You must use custom query with a projection. If EntityB property represents collection you can use something like:

var query = from a in context.EntitiesA
            select new 
               {
                  a.Id,
                  a.Name,
                  Bs = a.EntityB.Select(b => new { 
                       b.StuffToBeLoaded1, 
                       b.StuffToBeLoaded2 
                  })
               };

If EntityB is not a collection navigation property you can simply use:

var query = from a in context.EntitiesA
            select new 
               {
                  a.Id,
                  a.Name,
                  a.EntityB.StuffToBeLoaded1, 
                  a.EntityB.StuffToBeLoaded2 
               };           


来源:https://stackoverflow.com/questions/6290306/partial-eager-loading-child-entity-load-specific-fileds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!