开发者

Eager Loading on tracked items?

开发者 https://www.devze.com 2022-12-13 06:38 出处:网络
I have an element bound to an entity (C开发者_开发知识库ontact) that exposes some navigation properties.

I have an element bound to an entity (C开发者_开发知识库ontact) that exposes some navigation properties.

I want, that on some action (i.e. a "Load children" button), the Contact should load for all its children and grand children like I can do with an ObjectQuery.Include before the execution; example (pseudo):

DirectCast(element.DataContext, Contact).SubContacts. _
   Include("Address.State"). _
   Load()

I want to be able to shape the results also by reloading items like the example above, not only when querying the model itself.

I think this should of been a part of Entity-Framework in some way.


I'm not positive I follow the question, but let me check:

You want to take an entity that you have already loaded, then load a navigation property with include paths for that property. This isn't actually that difficult, as long as you are using the same context that you used to load the first object.

The important point is that whenever you do a query that returns an entity, Entity Framework will automatically link it up with any of its related objects that you already have. So what you really want is to produce a new query that returns the extra data, with some includes.

Every reference in Entity Framework has a method called CreateSourceQuery. It returns an Object Query, so you can use it to do this (sorry, C#):

AlreadyLoadedContact.SubContacts.CreateSourceQuery().Include("Address.State").ToList();

The ToList call executes the query, and Entity Framework will take all the results and automatically add them to you SubContacts EntityCollection.

0

精彩评论

暂无评论...
验证码 换一张
取 消