views:

259

answers:

3

I've finally managed to get a handle on loading information using Silverlight, ADO.NET Entities, and RIA Services, but I'm still having a problem with getting the information about the relationships.

For instance, imagine a product, and that product has several categories of attributes. We'll call them ProductAreas.

The Product object has a property called ProductAreas (as a result of their relationship), but when I call:

ctx.Load(GetProductsQuery());

(Where ctx is my DomainContext), the Product objects returned have the ProductAreas property, but it contains no elements, which is a very serious problem, in my case.

So the question is: How do I get access to these relationships?

+2  A: 

I'm not sure what your GetProductsQuery() method does, but you should be able use the .Include('ProductAreas') method in your query. If you update your question with the contents of that method I'll try to help more.

bendewey
Thanks, Ben! This is great. You led me exactly where I needed to go. I elaborated on your answer a little below, as just adding the include didn't haul in the information, but that in conjunction with the include attribute did.
Anthony Compton
+2  A: 

This isn't technically the way this system is supposed to work, but I wanted to expand on your answer, while at the same time giving it the credit it rightfully deserves for leading me where I needed to be.

The solutions was to, in the GetProductsQuery() method use

return this.ObjectContext.Products.Include("ProductAreas");

instead of

return this.ObjectContext.Products;

And in the metadata file, go to the Products class and, just above the ProductAreas property add [Include()], so that it looks like:

[Include()]
public EntityCollection<ProductAreas> ProductAreas;
Anthony Compton
A: 

But how do I do lazy loading? Every body seems to avoid this question and reply that i should do eager loading. But what if each product has a 1000 ProductArea's?

And if I have managed to populate aProduct.ProductArea and I add a new ProductArea what will happen when I call submitchanges? Will the adding be tracked?

NIck
In my experience (others may have a better way), but if you want to do lazy loading, you'll have to explicitly perform loads whenever you need to. Take for example a directory structure that you don't want to load all at once. When the user selects a directory, perform a load getting the children of that directory and only those items. And to answer the second question, yes. If you have a Product object that you've loaded and you create a new ProductArea object and attached it (product.ProductArea = new ProductArea()) the new object will get saved when you call SubmitChanges().
Anthony Compton