ASP.NET MVC,实体框架和性能

时间:2011-10-05 15:06:38

标签: asp.net-mvc entity-framework

我遇到性能问题但不了解EF的行为。我使用ASP.NET MVC应用程序,并在模型中包含以下代码:

public List<Portal> PortalListWithCategories()
{
    List<Portal> q = new List<Portal>();
        q = (from i in _dataContext.Portals.Include("Categories").Include("Ideas") where i.Categories.Count > 0 orderby i.DefaultPortal descending select i).ToList();
    return q;
}

我从控制器调用它:

        portalList = _repository.PortalListWithCategories();

据我所知,EF应该执行批量请求并返回具有嵌套集合“Categories”和“Ideas”的门户集合。

但是我看到了以下内容:

                @foreach (var category in portal.Categories.Where(n => n.Ideas.Count > 0 && n.Portals.Any(g => g.PortalID == portal.PortalID)))
                {
                    if ((from e in category.Ideas where e.Portals.Any(t => t.PortalID == portal.PortalID) select e).Count() > 0)
                    {
                        string categoryLink = Url.RouteUrl("Full", new { PortalID = portal.PortalID, CategoryID = category.CategoryID, action = "Ideas" });
                        List<NEOGOV_Ideas.Models.Ideas> ideas = category.Ideas.Where(o => o.Portals.Any(p => p.PortalID == portal.PortalID) && o.Categories.Any(h => h.CategoryID == category.CategoryID)).OrderByDescending(k => k.CreatedDateTime).ToList();
                    <div class="grid_4">
                        <h4>
                            <a href="@categoryLink">@category.CategoryName<span class="count_link">&nbsp;(@ideas.Count())</span>
                                <span class="follow_link">&raquo;</span></a></h4>
                        <ul>
                            @foreach (var idea in ideas.Take(3))
                            {
                                string ideaLink = Url.RouteUrl("IdeaShort", new { id = idea.IdeaID });
                                if (!idea.IdeaTypeReference.IsLoaded) { idea.IdeaTypeReference.Load(); }
                                string cssclass = " class=\"" + idea.IdeaType.TypeName.ToLower() + "\"";
                                <li><a href="@ideaLink" @cssclass>@idea.Title</a></li>
                            }
                        </ul>
                    </div>
                            if (i == 2)
                            {
                    <div class="clear">
                    </div>
                            }
                            i++;
                    }
                }

据我所知,我不应该对DB有新的请求,但我有很多。为什么呢?

[ADDED]

我找到了这个字符串 (来自e in category.Ideas,其中e.Portals.Any(t =&gt; t.PortalID == portal.PortalID)选择e).Count() 向DB生成许多请求,如:

exec sp_executesql N'SELECT [Extent2]。[PortalID] AS [PortalID], [Extent2]。[PortalName] AS [PortalName], [Extent2]。[DefaultPortal] AS [DefaultPortal] FROM [dbo]。[PortalIdeas] AS [Extent1] INNER JOIN [dbo]。[门户] AS [Extent2] ON [Extent1]。[PortalID] = [Extent2]。[PortalID] WHERE [Extent1]。[IdeaID] = @ EntityKeyValue1',N'@ EntityKeyValue1 int',@ EntityKeyValue1 = 5618

为什么它会发生在Count()?

第二个问题如何正确使用?

1 个答案:

答案 0 :(得分:1)

由于e.Portals未加载并且在foreach循环中,因此EF必须往返数据库以获取Idea的Portals
您也应该在查询中包含Ideas

我没有对此进行测试,但我认为您应该添加.Include("Ideas.Portals")(或者如果您使用EF 4.1,请添加using System.Data.Entity并使用.Include(c => c.Ideas.Portals))。