Entitiy Framework对象树加载 [英] Entitiy Framework object tree loading

查看:151
本文介绍了Entitiy Framework对象树加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在以下EF实体中加载:

How to load in a following EF entities:

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

假设我们有地址id,加载地址与人和宠物。如何做?

Let's say we have address id and we want to load address with person and the pets. How to 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);

我收到错误:

>指定的包含路径无效

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

这篇关于Entitiy Framework对象树加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆