Entity Framework 3.5 - How to load children

.net-3.5 entity-framework linq

Question

My questions is probably very simple, how do you load children/subclasses. There is no "load" or anything like it that I can find to have the context load the children.

the context class is of ObjectContext type, see below:

public partial class RTIPricingEntities : global::System.Data.Objects.ObjectContext

Product

Product.ModifiedByUser (how to load this class, when loading product??)

Product.Category (how to load the categories when loading product?)

1
10
8/28/2010 2:30:17 AM

Popular Answer

You can eager load:

var q = from p in Context.Products
                  .Include("ModifiedByUser")
                  .Include("Category")
        select p;

...or project:

var q = from p in Context.Products
        select new 
        {
           Id = p.Id,
           Name = p.Name
           ModifiedByUserName = p.ModifiedByUser.Name,
           CategoryName = p.Category.Name
        }

The advantage of projection is you get only the data you need, not the whole of every referenced entity. Advantage of eager loading is that returned entities do change tracking. Pick the right technique for the problem at hand.

Update

Yes, it is important to mention that you are using RIA Services. I presume you're also working within the client. This makes things completely different.

In RIA Services, it is very important to make sure that you return the entire graph of entities you require in the initial load. You don't want to call anything like .Load() on an entity, because that would be another hot to the server, which is bad for performance. If you are in, for example, a Silverlight client and request a list of instances from the server and their related properties are not already materialized, it is already too late. Also, Include will not work within a Silverlight client. Therefore, RIA Services has server-side tools you can use to ensure that you return the correct, fully materialized object graph initially.

What you need to do instead is used IncludeAttribute inside your RIA Services server. You can create a "buddy" metadata class to decorate your entity model with [Include]. There are examples in the RIA Services overview document, section 4.8.

16
9/4/2009 1:05:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow