【发布时间】:2016-03-25 07:26:51
【问题描述】:
我的问题是每次加载此视图时,我的应用程序都会向数据库发送 249 个相同的查询。本来我用的是懒加载,查询次数翻了一番。
上面的 249 代表这个查询返回的行数。
我的理解是 .Include 创建一个应该消除这种行为的连接?
谁能告诉我如何消除这种重复查询?
干杯!
下面的代码是伪代码,不能编译。
控制器:
var apples = _unitOfWork.Context.Apples
.Include(x=> x.AppleTypes)
.OrderByDescending(x => x.Id)
.Where(x => x.Status == (int)AppleStatusConstants.New
&& x.IsRejected != true && x.AppleManId != null);
return View(apples);
查看:
@model IEnumerable<Apple>
@Html.DisplayNameFor(model => model.AppleTypes.TypeSeason)
@foreach (var item in Model){
@Html.DisplayFor(modelItem => item.AppleTypes.TypeSeason)
}
来自 Glimpse 的 SQL 跟踪:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Type] AS [Type],
[Extent2].[Id] AS [Id1],
[Extent2].[TypeSeason] AS [TypeSeason],
FROM [dbo].[Apples] AS [Extent1]
LEFT OUTER JOIN [dbo].[AppleTypes] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
WHERE (0 = [Extent1].[Status]) AND ( NOT ((1 = [Extent1].[IsRejected]) AND ([Extent1].[IsRejected] IS NOT NULL))) AND ([Extent1].[OrgUnitId] IS NOT NULL)
ORDER BY [Extent1].[Id] DESC
【问题讨论】:
标签: asp.net-mvc entity-framework eager-loading