问题描述
我有一个产品实体,其中有 0 或 1 个畅销书"实体.出于某种原因,当我说:
I have a product entity, which has 0 or 1 "BestSeller" entities. For some reason when I say:
db.Products.OrderBy(p => p.BestSeller.rating).ToList();
我得到的 SQL 有一个额外的"外连接(如下).如果我添加第二个 0 或 1 关系,并按两者排序,那么我会得到 4 外连接.似乎每个这样的实体都在产生 2 个外部联接而不是一个.LINQ to SQL 的行为完全符合您的预期,没有额外的连接.
the SQL I get has an "extra" outer join (below). And if I add on a second 0 or 1 relation ship, and order by both, then I get 4 outer joins. It seems like each such entity is producing 2 outer joins rather than one. LINQ to SQL behaves exactly as you'd expect, with no extra join.
有没有其他人遇到过这种情况,或者知道如何解决?
Has anyone else experienced this, or know how to fix it?
SELECT
[Extent1].[id] AS [id],
[Extent1].[ProductName] AS [ProductName]
FROM [dbo].[Products] AS [Extent1]
LEFT OUTER JOIN [dbo].[BestSeller] AS [Extent2] ON [Extent1].[id] = [Extent2].[id]
LEFT OUTER JOIN [dbo].[BestSeller] AS [Extent3] ON [Extent2].[id] = [Extent3].[id]
ORDER BY [Extent3].[rating] ASC
推荐答案
额外的外连接看起来确实是多余的.我觉得最好联系实体框架设计团队.他们可能知道这是否是一个错误,并查看它是否需要在下一个版本中解决.您可以通过 http://blogs.msdn.com/b/efdesign/contact 与他们联系.aspx
That extra outer join does seem quite superfluous. I think it's best to contact the entity framework design team. They may know if it's a bug and see if it something that needs to be resolved in a next version. You can contact them at http://blogs.msdn.com/b/efdesign/contact.aspx
这篇关于Entity Framework 4 中的左外连接过多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!