开发者

Entitiy Framework object tree loading

How to load in a following EF entities:

Entitiy Framework object tree loading

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.


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


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;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜