0

我有以下表格:-

在此处输入图像描述

以及以下操作方法:-

 public ViewResult Index()
        {
            return View(groupRepository.AllIncluding(group => group.SecurityRoles));
        }

以及以下存储库:-

 public IQueryable<Group> AllIncluding(params Expression<Func<Group, object>>[] includeProperties)
        {
            IQueryable<Group> query = context.Groups;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

导航到操作方法时生成 SQL(使用 SQL 服务器分析器)是:;两个 SQL 批处理和一个 RPC。如下:-

SELECT 
[Project1].[GroupID] AS [GroupID], 
[Project1].[Name] AS [Name], 
[Project1].[Description] AS [Description], 
[Project1].[C1] AS [C1], 
[Project1].[SecurityRoleID] AS [SecurityRoleID], 
[Project1].[Name1] AS [Name1], 
[Project1].[Description1] AS [Description1]
FROM ( SELECT 
    [Extent1].[GroupID] AS [GroupID], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Description] AS [Description], 
    [Join1].[SecurityRoleID1] AS [SecurityRoleID], 
    [Join1].[Name] AS [Name1], 
    [Join1].[Description] AS [Description1], 
    CASE WHEN ([Join1].[SecurityRoleID2] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[Groups] AS [Extent1]
    LEFT OUTER JOIN  (SELECT [Extent2].[SecurityRoleID] AS [SecurityRoleID2], [Extent2].[GroupID] AS [GroupID], [Extent3].[SecurityRoleID] AS [SecurityRoleID1], [Extent3].[Name] AS [Name], [Extent3].[Description] AS [Description]
        FROM  [dbo].[SecurityRoleGroups] AS [Extent2]
        INNER JOIN [dbo].[SecurityRoles] AS [Extent3] ON [Extent3].[SecurityRoleID] = [Extent2].[SecurityRoleID] ) AS [Join1] ON [Extent1].[GroupID] = [Join1].[GroupID]
)  AS [Project1]
ORDER BY [Project1].[GroupID] ASC, [Project1].[C1] ASC



SELECT 
[Project1].[GroupID] AS [GroupID], 
[Project1].[Name] AS [Name], 
[Project1].[Description] AS [Description], 
[Project1].[C1] AS [C1], 
[Project1].[SecurityRoleID] AS [SecurityRoleID], 
[Project1].[Name1] AS [Name1], 
[Project1].[Description1] AS [Description1]
FROM ( SELECT 
    [Extent1].[GroupID] AS [GroupID], 
    [Extent1].[Name] AS [Name], 
    [Extent1].[Description] AS [Description], 
    [Join1].[SecurityRoleID1] AS [SecurityRoleID], 
    [Join1].[Name] AS [Name1], 
    [Join1].[Description] AS [Description1], 
    CASE WHEN ([Join1].[SecurityRoleID2] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[Groups] AS [Extent1]
    LEFT OUTER JOIN  (SELECT [Extent2].[SecurityRoleID] AS [SecurityRoleID2], [Extent2].[GroupID] AS [GroupID], [Extent3].[SecurityRoleID] AS [SecurityRoleID1], [Extent3].[Name] AS [Name], [Extent3].[Description] AS [Description]
        FROM  [dbo].[SecurityRoleGroups] AS [Extent2]
        INNER JOIN [dbo].[SecurityRoles] AS [Extent3] ON [Extent3].[SecurityRoleID] = [Extent2].[SecurityRoleID] ) AS [Join1] ON [Extent1].[GroupID] = [Join1].[GroupID]
)  AS [Project1]
ORDER BY [Project1].[GroupID] ASC, [Project1].[C1] ASC



exec sp_executesql N'SELECT 
[Extent1].[GroupID] AS [GroupID], 
[Extent1].[UserName] AS [UserName]
FROM [dbo].[UserGroups] AS [Extent1]
WHERE [Extent1].[GroupID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1

但我有以下问题:-

  1. 为什么会生成两个 SQL 批处理。
  2. 当我发布了

    @(item.UserGroups == null ? "None" : item.UserGroups.Count.ToString())

在视图上,它将检索表中的所有记录,而不是仅计算数据库中的记录,然后返回计数。

  1. 为什么在 Scaffolding 生成的代码中,只会传递 SecurityRole 导航属性而忽略了 UserGroups 导航属性?
4

1 回答 1

1

尝试将您的模型传递给查看,List而不是IQueryable

return View(groupRepository.AllIncluding(group => group.SecurityRoles).ToList());
于 2013-07-10T13:03:26.277 回答