在玩 EF5 时,我发现了一些奇怪的东西。给定这两个类和一个简单的查询
public class Customer {
public string Name { get; set; }
public string Email { get; set; }
...
public virtual IList<Order> Orders { get; set; }
}
public class Order {
public Customer Cust { get; set; }
public Datetime DateOrdered { get; set; }
...
}
using (var ctx = new DatabaseContext(connstring))
{
Customer c= ctx.customers.Include(x => x.orders).Where(x => x.Id == 1).Single<Customer>();
foreach (Order o in c.orders)
{
Console.WriteLine(o.ToString());
}
}
我检查了 SQL Server Profiler 输出,我认为查询是不必要的复杂:
SELECT
[Project1].[Id] AS [Id],
[Project1].[name] AS [name],
[Project1].[C1] AS [C1],
[Project1].[Id1] AS [Id1],
[Project1].[customer_Id] AS [customer_Id],
[Project1].[timeordered] AS [timeordered]
FROM ( SELECT
[Limit1].[Id] AS [Id],
[Limit1].[name] AS [name],
[Extent2].[Id] AS [Id1],
[Extent2].[customer_Id] AS [customer_Id],
[Extent2].[timeordered] AS [timeordered],
CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM (SELECT TOP (2) [Extent1].[Id] AS [Id], [Extent1].[name] AS [name]
FROM [dbo].[Customers] AS [Extent1]
WHERE 1 = [Extent1].[Id] ) AS [Limit1]
LEFT OUTER JOIN [dbo].[Orders] AS [Extent2] ON [Limit1].[Id] = [Extent2].[customer_Id]
) AS [Project1]
ORDER BY [Project1].[Id] ASC, [Project1].[C1] ASC
我确定我错过了一些东西,但我不知道是什么,或者这是急切加载的正常行为?