我需要在我的 EntityFramework 查询 (4.1) 中包括孙子,其中孙子是单身离开孩子,如下所示:
Schedule 有一个 Route,Route 有一个 FromAirport 和一个 ToAirport
我尝试了以下方法:
.Include(x => x.Route.FromAirport).Include(x => x.Route.ToAirport)
但它会产生如下的 sql 查询:
FROM [dbo].[Schedule] AS [Extent1]
INNER JOIN [dbo].[Route] AS [Extent2] ON [Extent1].[RouteId] = [Extent2].[Id]
LEFT OUTER JOIN [dbo].[Route] AS [Extent3] ON [Extent1].[RouteId] = [Extent3].[Id]
LEFT OUTER JOIN [dbo].[Airport] AS [Extent4] ON [Extent3].[FromAirportId] = [Extent4].[Id]
LEFT OUTER JOIN [dbo].[Airport] AS [Extent5] ON [Extent3].[ToAirportId] = [Extent5].[Id]
路线当然要加入两次。只要 EF 正确绑定到实体模型,我就可以忍受这一点。
有没有更好的方法来指定包含?