实体框架-包含多个级别的属性 [英] Entity Framework - Include multiple level properties

查看:64
本文介绍了实体框架-包含多个级别的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用实体框架从数据库中获取表。

I am trying to get a table from database using entity framework.

该表引用了其他表,该表又引用了其他表。
我知道如何包括其他表。并根据答案和此 MSDN页面包括多个级别

The table has reference to other table which again has reference to other tables. I know how to include other tables. And according to this answer and this MSDN page including multiple levels are like this:

entity.TableLevel1.Include(tLvl1=>tLvl1.TableLevel2.Select(tLvl2=>tLvl2.TableLevel3));

但是我的问题是,如何在3级包含另一个表?

But my question is, how to include another table at level 3 ?

这似乎不起作用:

entity.TableLevel1
          .Include(tLvl1=>tLvl1.TableLevel2
               .Select(tLvl2=>tLvl2.TableLevel3)
               .Select(tLvl2 => tLvl2.AnotherTableLevel3);


推荐答案

添加另一个 Include 调用:

entity.TableLevel1.Include(tLvl1=>tLvl1.TableLevel2.Select(tLvl2=>tLvl2.TableLevel3))
                  .Include(tLvl1=>tLvl1.TableLevel2.Select(tLvl2=>tLvl2.AnotherTableLevel3));

如果要加载相同级别的相关实体,应为每个实体调用 Include 扩展方法。

If you want to load related entities that are at the same level you should call Include extension method for each of them.

这篇关于实体框架-包含多个级别的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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