开发者

Entitiy Framework object tree loading

开发者 https://www.devze.com 2022-12-17 00:30 出处:网络
How to load in a following EF entities: image source: http://blogs.microsoft.co.il/blogs/idof/archive/2008/08/20/entity-framework-and-lazy-loading.aspx

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

精彩评论

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

关注公众号