EF4 中的数据加载策略/语法
潜伏很久了,第一次发帖,新学习EF4和MVC3。
我需要帮助确保我在这种情况下使用正确的数据加载策略,以及一些帮助最终确定查询的一些细节。我目前正在使用此处概述的急切加载方法“仪表板”视图,需要来自大约 10 个表(都具有 FK 关系)的少量数据。
var query = from l in db.Leagues
.Include("Sport")
.Include("LeagueContacts")
.Include("LeagueContacts.User")
.Include("LeagueContacts.User.UserContactDatas")
.Include("LeagueEvents")
.Include("LeagueEvents.Event")
.Include("Seasons")
.Include("Seasons.Divisions")
.Include("Seasons.Divisions.Teams")
.Where(l => l.URLPart.Equals(leagueName))
select (l);
model = (Models.League) query.First();
然而,我需要对我无法解决的数据进行一些额外的过滤、排序和整形。以下是我目前的主要需求/担忧:
几个子对象仍然需要额外的过滤,但我还无法找出语法或最佳方法。示例:“TOP 3 LeagueEvents.Event WHERE StartDate >= getdate() ORDER BY LeagueEvents.Event.StartDate”
我需要对某些字段进行排序。示例:ORDERBY Seasons.StartDate、LeagueEvents.Event.StartDate 和 LeagueContacts.User.SortOrder 等。
我已经非常关心此查询生成的 SQL 的总体大小和联接数量,并且正在考虑我可能需要一种不同的数据加载方法。(显式加载?多个 QueryObjects?POCO?)
有关如何解决这些剩余需求以及确保非常感谢最佳表现。
Long time lurker, first time posting, and newly learning EF4 and MVC3.
I need help making sure I'm using the correct data loading strategy in this case as well as some help finalizing some details of the query. I'm currently using the eager loading approach outlined here for somewhat of a "dashboard" view that requires a small amount of data from about 10 tables (all have FK relationships).
var query = from l in db.Leagues
.Include("Sport")
.Include("LeagueContacts")
.Include("LeagueContacts.User")
.Include("LeagueContacts.User.UserContactDatas")
.Include("LeagueEvents")
.Include("LeagueEvents.Event")
.Include("Seasons")
.Include("Seasons.Divisions")
.Include("Seasons.Divisions.Teams")
.Where(l => l.URLPart.Equals(leagueName))
select (l);
model = (Models.League) query.First();
However, I need to do some additional filtering, sorting, and shaping of the data that I haven't been able to work out. Here are my chief needs/concerns from this point:
Several child objects still need additional filtering but I haven't been able to figure out the syntax or best approach yet. Example: "TOP 3 LeagueEvents.Event WHERE StartDate >= getdate() ORDER BY LeagueEvents.Event.StartDate"
I need to sort some of the fields. Examples: ORDERBY Seasons.StartDate, LeagueEvents.Event.StartDate, and LeagueContacts.User.SortOrder, etc.
I'm already very concerned about the overall size of the SQL generated by this query and the number of joins and am thinking that I may need a different data loading approach alltogether.(Explicit loading? Multiple QueryObjects? POCO?)
Any input, direction, or advice on how to resolve these remaining needs as well as ensuring the best performance is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对查询大小和 结果集的大小是有形的。
正如 @BrokenGlass 提到的,EF 不允许您对包含进行过滤或排序。如果要排序或过滤关系,则必须使用匿名类型或自定义(非映射)类型的投影:
Your concern about size of the query and size of the result set are tangible.
As @BrokenGlass mentioned EF doesn't allow you doing filtering or ordering on includes. If you want to order or filter relations you must use projection either to anonymous type or custom (non mapped) type:
不幸的是,EF 不允许使用其导航属性有选择地加载相关实体,如果您指定
Include("Foo")
,它将始终加载所有Foos
。您必须使用
Where()
子句作为它们应用的过滤器来对每个相关实体进行联接。Unfortunately EF doesn't allow to selectively load related entities using its navigation properties, it will always load all
Foos
if you specifyInclude("Foo")
.You will have to do a join on each of the related entities using your
Where()
clauses as filters where they apply.