Entitiy Framework对象树加载

Pet*_*nar 1 c# entity-framework

如何加载以下EF实体:

替代文字

图片来源:http://blogs.microsoft.co.il/blogs/idof/archive/2008/08/20/entity-framework-and-lazy-loading.aspx

假设我们有地址ID,我们想加载人和宠物的地址.怎么做?

我们能做到这一点

var address = contex.Addresses.Include("Peson").Where(add => add.Id == GivenId);
Run Code Online (Sandbox Code Playgroud)

但它加载地址和没有宠物的人.

如果我包含宠物实体,如下所示:

var address = contex.Addresses.Include("Peson").Include("Pets").Where(add => add.Id == GivenId);
Run Code Online (Sandbox Code Playgroud)

我收到错误:

指定的包含路径无效.

所以问题是如何加载整个实体树.

Luc*_*cas 5

您可以通过用"."分隔关系来加载树.

context.Address.Include("Person.Pets"); //Include all the persons with their pets
context.Pets.Include("Person.Address"); //Include all the persons with their addresses
Run Code Online (Sandbox Code Playgroud)