实体框架:获取的对象不直接相关 [英] Entity Framework: Getting objects not immediately related

查看:159
本文介绍了实体框架:获取的对象不直接相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架为我的.NET应用程序。我已经能够返回对象及其直接相关的对象以及(很方便),但我有麻烦的那些对象的对象。

I am using Entity Framework for my .NET application. I have been able to return objects and their directly-related objects as well (very convenient), but I am having trouble getting the objects of those objects.

 IEnumerable<Lot> i = (((ObjectSet<Car>)_carRepository.GetQuery())
                                    .Include(a => a.CarTypes).Take(10).ToList()

这工作,我可以访问carTypes,但是我无法弄清楚如何访问与CarTypes关联的表(例如具有与汽车相关的类型字段的表)。

This works and I can access carTypes, however I cannot figure out how to access tables associated with CarTypes (e.g. tables which have fields associated with the car types).

我试图用一个加入,但是我无法弄清楚如何得到它的工作的权利。

I tried to use a Join however I was unable to figure out how to get it to work right.

所有帮助AP preciated。

All help appreciated.

推荐答案

包括可以链接,但你必须记住,它使​​用的字符串,而不是lambda表达式。所以,你如果你想要一个两级办.INCLUDE(CarTypes.Company),包括。你也可以连接包括语句这意味着包括来自同一顶级多个分支 - 例如。 .INCLUDE(CarTypes)。包括(SomeOtherTypeFromTheSameParentAsCar)。

Include can be chained, but you have to keep in mind that it uses strings rather than lambdas. So you do .Include("CarTypes.Company") if you want a two level include. You can also chain Include statements which means including more than one branch from the same top level--eg. .Include("CarTypes").Include("SomeOtherTypeFromTheSameParentAsCar").

您应该记住,虽然,深包含因的每一个部分包含只是增加了到一个查询所构建,所以你会得到一个更大和更复杂的查询语句可能不会产生最好的性能而做了一大堆的幕后联接。有时做一些包括在一个查询,然后发出第二个查询的更有效,让您的数据的其余部分或类似的东西。

You should keep in mind, though, that deep Include statements may not produce the best possible performance because every part of an Include just adds on to the one query that you are building so you will get a larger and more complex query which does a whole bunch of joins under the covers. Sometimes it's more effective to do a few Includes in one query and then issue a second query to get the rest of your data or something like that.

使用EF4你还可以设置延迟加载有时会做出这种事情更容易(当然它产生的多次往返,而不是一两个非常大的往返)。

With EF4 you can also setup lazy loading which can sometimes make this kind of thing even easier (but of course it produces multiple roundtrips rather than one or two very large roundtrips).

这篇关于实体框架:获取的对象不直接相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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