У меня очень простые модели
.NET Core 2.1 / EF Core 2.1 / MSSQL
public class ImageZ
{
[Key]
public Guid Id { get; set; }
public string Base64 { get; set; }
public string Title { get; set; }
}
public class Gallery
{
[Key]
public Guid Id { get; set; }
public ImageZ MainImage { get; set; }
public List<ImageZ> Images { get; set; } = new List<ImageZ>();
}
и я использую этот LINQ, чтобы загрузить его из db
return _context
.Gallery
.Include(x => x.Images)
.Include(x => x.MainImage)
.OrderBy(x => Guid.NewGuid())
.FirstOrDefault();
Но он отправляет два запроса в БД
SELECT TOP(1) [x].[Id], [x].[MainImageId], [x.MainImage].[Id], [x.MainImage].[Base64], [x.MainImage].[GalleryId], [x.MainImage].[Title]
FROM [Gallery] AS [x]
LEFT JOIN [ImageZ] AS [x.MainImage] ON [x].[MainImageId] = [x.MainImage].[Id]
ORDER BY NEWID(), [x].[Id]
SELECT [x.Images].[Id], [x.Images].[Base64], [x.Images].[GalleryId], [x.Images].[Title]
FROM [ImageZ] AS [x.Images]
INNER JOIN (
SELECT DISTINCT [t].*
FROM (
SELECT TOP(1) [x0].[Id], NEWID() AS [c]
FROM [Gallery] AS [x0]
LEFT JOIN [ImageZ] AS [x.MainImage0] ON [x0].[MainImageId] = [x.MainImage0].[Id]
ORDER BY [c], [x0].[Id]
) AS [t]
) AS [t0] ON [x.Images].[GalleryId] = [t0].[Id]
ORDER BY [t0].[c], [t0].[Id]
Это правильное поведение? Разве это не должно быть сделано только с одним?