EF7是否完全支持导航属性和自定义投影?或者我可能误解了如何构造此查询。 Study
实体具有可为空的ProjectId
和相应的虚拟Project
导航属性。 Project
实体具有不可为空的CategoryId
和Category
导航属性。使用ef
命令对实体进行反向搭建。
如果我运行以下查询:
return _context.Study
.Include(s => s.Project)
.ThenInclude(p => p.Category)
.Select(s => new Models.StudySearchResult
{
StudyId = s.StudyId,
MasterStudyId = s.MasterStudyId,
ShortTitle = s.ShortTitle,
Category = s.Project == null ? string.Empty : s.Project.Category.CategoryDesc,
SubmitterId = s.SubmitterId
}).ToList();
EF7错误地生成以下SQL,该SQL使用INNER JOIN
而不是LEFT JOIN
:
SELECT [s].[StudyId]
,[s].[MasterStudyId]
,[s].[ShortTitle]
,CASE WHEN [s].[ProjectId] IS NULL THEN @__Empty_0 ELSE [s.Project.Category].[CategoryDesc] END
,[s].[SubmitterId]
FROM [Study] AS [s]
INNER JOIN [Project] AS [s.Project]
ON [s].[ProjectId] = [s.Project].[ProjectId]
INNER JOIN [Category] AS [s.Project.Category]
ON [s.Project].[CategoryId] = [s.Project.Category].[CategoryId]
答案 0 :(得分:2)
我有同样的问题。我发现EF7中存在当前未解决的问题,即为可选导航生成SQL。它将在RC2中修复。
https://github.com/aspnet/EntityFramework/issues/4205 https://github.com/aspnet/EntityFramework/issues/3186