My issue is that every time I load this view my application sends 249 identical queries to the database. Originally I was using lazy loading and the number of queries was double.
The 249 number above represents the number of rows this query returns.
My understanding is that .Include creates a join which should eliminate this behavior?
Can anyone tell me how to eliminate this duplication of queries?
Cheers!
The code below is pseudo code and is not intended to compile.
Controller:
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);
View:
@model IEnumerable<Apple>
@Html.DisplayNameFor(model => model.AppleTypes.TypeSeason)
@foreach (var item in Model){
@Html.DisplayFor(modelItem => item.AppleTypes.TypeSeason)
}
SQL Trace from Glimpse:
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
