views:

86

answers:

2

How to load in a following EF entities:

alt text

image source: http://blogs.microsoft.co.il/blogs/idof/archive/2008/08/20/entity-framework-and-lazy-loading.aspx

Let's say we have address id and we want to load address with person and the pets. How to do that?

We can do that

var address = contex.Addresses.Include("Peson").Where(add => add.Id == GivenId);

But it loads address and person w/o pets.

If I include a pets entity, like this:

var address = contex.Addresses.Include("Peson").Include("Pets").Where(add => add.Id == GivenId);

I get error:

A specified Include path is not valid.

So the question is how to load a whole entity tree.

+1  A: 

Always select from top level object down, something like:

var person = from p in context.Person.Include("Pets").Include("Address")
where p.Address.Id == givenId
select p;
TFD
Let's say I want to load objects thru Addresses entity.
Peter Stegnar
You can probably mangle the LINQ to do that, but it would be confusing. The relationship between Pets and Address is Person. You can't have you cake and it it too!Just iterate through "person.Address", and then you can get the Person and Pet for each address
TFD
+2  A: 

You can load the tree by separating the relationships with a "."

context.Address.Include("Person.Pets"); //Include all the persons with their pets
context.Pets.Include("Person.Address"); //Include all the persons with their addresses
Lucas