我必須有一個略有不同的情況下,有一個ToList()在那裏的某個地方,或相似的,因爲我無法重現的場景我有一個問題上的東西:
public class FooBarRepo : IFooBarRepo
{
private readonly Entities _context;
public FooBarRepo()
{
this._context = new Entities();
}
public IQueryable<FooBar> Get()
{
return this._context.FooBar;
}
public IQueryable<FooBar> GetIncludeFoo()
{
return this.Get().Include(i => i.Foo);
}
public IQueryable<FooBar> GetIncludeBar()
{
return this.Get().Include(i => i.Bar);
}
public IQueryable<FooBar> GetIncludeFooAndBar()
{
return this
.Get()
.Include(i => i.Foo)
.Include(i => i.Bar);
}
public IQueryable<FooBar> GetIncludeFooAndChainBar()
{
return this.GetIncludeBar().Include(i => i.Foo);
}
public void Dispose()
{
this._context.Dispose();
}
}
class Program
{
static void Main(string[] args)
{
IEnumerable<FooBar> get;
IEnumerable<FooBar> getIncludeFoo;
IEnumerable<FooBar> getIncludeBar;
IEnumerable<FooBar> getIncludeFooAndBar;
IEnumerable<FooBar> getIncludeFooAndChainBar;
using (var context = new EntityFrameworkTesting.TestIncludeChaining.Repository.FooBarRepo())
{
get = context.Get().ToList();
getIncludeFoo = context.GetIncludeFoo().ToList();
getIncludeBar = context.GetIncludeBar().ToList();
getIncludeFooAndBar = context.GetIncludeFooAndBar().ToList();
getIncludeFooAndChainBar = context.GetIncludeFooAndChainBar().ToList();
}
}
}
生成的SQL:
-- get
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name]
FROM [dbo].[_FooBar] AS [Extent1]
-- getIncludeFoo
SELECT
[Project1].[Id] AS [Id],
[Project1].[Name] AS [Name],
[Project1].[C1] AS [C1],
[Project1].[Id1] AS [Id1],
[Project1].[FooBarId] AS [FooBarId]
FROM (SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent2].[Id] AS [Id1],
[Extent2].[FooBarId] AS [FooBarId],
CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[_FooBar] AS [Extent1]
LEFT OUTER JOIN [dbo].[_Foo] AS [Extent2] ON [Extent1].[Id] = [Extent2].[FooBarId]
) AS [Project1]
ORDER BY [Project1].[Id] ASC, [Project1].[C1] ASC
-- getIncludeBar
SELECT
[Project1].[Id] AS [Id],
[Project1].[Name] AS [Name],
[Project1].[C1] AS [C1],
[Project1].[Id1] AS [Id1],
[Project1].[FooBarId] AS [FooBarId]
FROM (SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent2].[Id] AS [Id1],
[Extent2].[FooBarId] AS [FooBarId],
CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[_FooBar] AS [Extent1]
LEFT OUTER JOIN [dbo].[_Bar] AS [Extent2] ON [Extent1].[Id] = [Extent2].[FooBarId]
) AS [Project1]
ORDER BY [Project1].[Id] ASC, [Project1].[C1] ASC
-- getIncludeFooAndBar
SELECT
[UnionAll1].[Id] AS [C1],
[UnionAll1].[Id1] AS [C2],
[UnionAll1].[Name] AS [C3],
[UnionAll1].[C1] AS [C4],
[UnionAll1].[Id2] AS [C5],
[UnionAll1].[FooBarId] AS [C6],
[UnionAll1].[C2] AS [C7],
[UnionAll1].[C3] AS [C8]
FROM (SELECT
CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1],
[Extent1].[Id] AS [Id],
[Extent1].[Id] AS [Id1],
[Extent1].[Name] AS [Name],
[Extent2].[Id] AS [Id2],
[Extent2].[FooBarId] AS [FooBarId],
CAST(NULL AS int) AS [C2],
CAST(NULL AS int) AS [C3]
FROM [dbo].[_FooBar] AS [Extent1]
LEFT OUTER JOIN [dbo].[_Foo] AS [Extent2] ON [Extent1].[Id] = [Extent2].[FooBarId]
UNION ALL
SELECT
2 AS [C1],
[Extent3].[Id] AS [Id],
[Extent3].[Id] AS [Id1],
[Extent3].[Name] AS [Name],
CAST(NULL AS int) AS [C2],
CAST(NULL AS int) AS [C3],
[Extent4].[Id] AS [Id2],
[Extent4].[FooBarId] AS [FooBarId]
FROM [dbo].[_FooBar] AS [Extent3]
INNER JOIN [dbo].[_Bar] AS [Extent4] ON [Extent3].[Id] = [Extent4].[FooBarId]) AS [UnionAll1]
ORDER BY [UnionAll1].[Id1] ASC, [UnionAll1].[C1] ASC
-- getIncludeFooAndChainBar
SELECT
[UnionAll1].[Id] AS [C1],
[UnionAll1].[Id1] AS [C2],
[UnionAll1].[Name] AS [C3],
[UnionAll1].[C1] AS [C4],
[UnionAll1].[Id2] AS [C5],
[UnionAll1].[FooBarId] AS [C6],
[UnionAll1].[C2] AS [C7],
[UnionAll1].[C3] AS [C8]
FROM (SELECT
CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1],
[Extent1].[Id] AS [Id],
[Extent1].[Id] AS [Id1],
[Extent1].[Name] AS [Name],
[Extent2].[Id] AS [Id2],
[Extent2].[FooBarId] AS [FooBarId],
CAST(NULL AS int) AS [C2],
CAST(NULL AS int) AS [C3]
FROM [dbo].[_FooBar] AS [Extent1]
LEFT OUTER JOIN [dbo].[_Bar] AS [Extent2] ON [Extent1].[Id] = [Extent2].[FooBarId]
UNION ALL
SELECT
2 AS [C1],
[Extent3].[Id] AS [Id],
[Extent3].[Id] AS [Id1],
[Extent3].[Name] AS [Name],
CAST(NULL AS int) AS [C2],
CAST(NULL AS int) AS [C3],
[Extent4].[Id] AS [Id2],
[Extent4].[FooBarId] AS [FooBarId]
FROM [dbo].[_FooBar] AS [Extent3]
INNER JOIN [dbo].[_Foo] AS [Extent4] ON [Extent3].[Id] = [Extent4].[FooBarId]) AS [UnionAll1]
ORDER BY [UnionAll1].[Id1] ASC, [UnionAll1].[C1] ASC
如果我再次遇到了無法找到的實際場景,我會更新/詢問一個新問題。但在此期間,感謝您的理智檢查。
當你調用'Include'兩次?例如:'this.Include(x => x.Bars).Include(x => x.Foos);'。 – haim770
看起來它應該對我有用。看看你是否可以將SQL發送到數據庫(通過分析器,日誌輸出等) – JasonCoder
@haim770,它可以像你期望的那樣工作,兩者都將包括在內 - 這是一個希望理解原因的簡單示例,但是(包括兩個,包括相同的功能)是我最終選擇的解決方案。 – Kritner