EF 7 - 导航属性 - 不正确的SQL?

时间:2016-02-11 23:40:50

标签: entity-framework entity-framework-core

EF7是否完全支持导航属性和自定义投影?或者我可能误解了如何构造此查询。 Study实体具有可为空的ProjectId和相应的虚拟Project导航属性。 Project实体具有不可为空的CategoryIdCategory导航属性。使用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]

1 个答案:

答案 0 :(得分:2)

我有同样的问题。我发现EF7中存在当前未解决的问题,即为可选导航生成SQL。它将在RC2中修复。

https://github.com/aspnet/EntityFramework/issues/4205 https://github.com/aspnet/EntityFramework/issues/3186