EF4 中的数据加载策略/语法

发布于 2024-11-17 04:30:03 字数 1334 浏览 4 评论 0原文

潜伏很久了,第一次发帖,新学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-11-24 04:30:03

您对查询大小和 结果集的大小是有形的

正如 @BrokenGlass 提到的,EF 不允许您对包含进行过滤或排序。如果要排序或过滤关系,则必须使用匿名类型或自定义(非映射)类型的投影:

        var query = db.Leagues
                      .Where(l => l.URLPart.Equals(leagueName))
                      .Select(l => new 
                          {
                              League = l,
                              Events = l.LeagueEvents.Where(...)
                                                     .OrderBy(...)
                                                     .Take(3)
                                                     .Select(e => e.Event)
                              ... 
                          });

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:

        var query = db.Leagues
                      .Where(l => l.URLPart.Equals(leagueName))
                      .Select(l => new 
                          {
                              League = l,
                              Events = l.LeagueEvents.Where(...)
                                                     .OrderBy(...)
                                                     .Take(3)
                                                     .Select(e => e.Event)
                              ... 
                          });
情未る 2024-11-24 04:30:03

不幸的是,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 specify Include("Foo").

You will have to do a join on each of the related entities using your Where() clauses as filters where they apply.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文