我有以下课程,例如
public class Team
{
[Key]
public virtual Int32 TeamId { get; set; }
[Required]
public virtual String Name { get; set; }
public virtual String Description { get; set; }
public virtual ICollection<TeamFeed> TeamFeeds { get; set; }
}
public class TeamFeed
{
public Int32 TeamFeedId { get; set; }
[Required]
public Int32 TeamId { get; set; }
public virtual bool IsEnabled { get; set; }
public virtual Team Team { get; set; }
}
我有LazyLoadingEnabled = false
和ProxyCreationEnabled = false
当我做
var team = db.Teams.Where(x => x.TeamId == 1).Include(x=>x.TeamFeeds);
EF 生成的 SQL 如下所示:
SELECT
[Project1].[TeamId] AS [TeamId],
[Project1].[Name] AS [Name],
[Project1].[Description] AS [Description],
[Project1].[C1] AS [C1],
[Project1].[TeamFeedId] AS [TeamFeedId],
[Project1].[TeamId1] AS [TeamId1],
[Project1].[IsEnabled] AS [IsEnabled],
FROM ( SELECT
[Extent1].[TeamId] AS [TeamId],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent2].[TeamFeedId] AS [TeamFeedId],
[Extent2].[TeamId] AS [TeamId1],
[Extent2].[IsEnabled] AS [IsEnabled],
CASE WHEN ([Extent2].[TeamFeedId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[Teams] AS [Extent1]
LEFT OUTER JOIN [dbo].[TeamFeeds] AS [Extent2] ON [Extent1].[TeamId] = [Extent2].[TeamId]
WHERE 1 = [Extent1].[TeamId]
) AS [Project1]
ORDER BY [Project1].[TeamId] ASC, [Project1].[C1] ASC
我不明白为什么它没有使用我会使用的 SQL 并创建了一个冗长的 SQL 语句。
SELECT
* --use all just for clarity in the example
FROM TEAMS T
INNER JOIN TEAMFEEDS TF
ON T.TEAMID = TF.TEAMID
ORDER BY
T.TEAMID
我是 EF 的新手,所以我可能不理解某些东西。
谢谢