如何实体框架递归层次合作?包括()似乎不与它合作 [英] How does Entity Framework work with recursive hierarchies? Include() seems not to work with it

查看:157
本文介绍了如何实体框架递归层次合作?包括()似乎不与它合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目项目有一个类别

类别 ID 名称,<强> 孩子 孩子类别

在我做的LINQ to Entities查询的特定项目,它不返回相关的类别 ,除非我使用包含(类别)方法。不过,这并不带来全品类,其父母和孩子。我可以做包含(Category.Parent),但是这个对象是像一棵树,我有一个递归层次结构,我不知道它在哪里结束。

When I do a LINQ to Entities query for a specific Item, it doesn't return the related Category, unless I use the Include("Category") method. But it doesn't bring the full category, with its parent and children. I could do Include("Category.Parent"), but this object is something like a tree, I have a recursive hierarchy and I don't know where it ends.

我怎样才能使EF完全加载类别,与父母和孩子,并与他们的父母和子女的父或母,等等?

How can I make EF fully load the Category, with parent and children, and the parent with their parent and children, and so on?

这是不是为整个应用程序,出于性能考虑,这将只需要为这个特定实体的类别。

This is not something for the whole application, for performance considerations it would be needed only for this specific entity, the Category.

推荐答案

加载

您可以再通过所有的孩子加载自己的孩子做一个为每循环。然后做一个对每个通过他们的孩子,等等。

You could then do a for each and loop through all the children loading their children. Then do a for each through their children, and so on.

级别数下来,你去将在每个循环你有一些硬codeD。

The number of levels down you go will be hard coded in the number of for each loops you have.

下面是使用负载的例子: http://msdn.microsoft。 COM / EN-US /库/ bb896249.aspx

Here is an example of using load: http://msdn.microsoft.com/en-us/library/bb896249.aspx

这篇关于如何实体框架递归层次合作?包括()似乎不与它合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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