我的数据库中有一个1-1关系,当我使用.Include()
时,EF生成一个可怕的查询,需要很长时间才能执行(多个连接)。所以我想 - 我会编写自己的查询来加载这些实体。我尝试了什么:
.Include()
,那么EF会生成相同的可怕查询。所以,问题是 - 有没有办法实现绕过EF生成的SQL查询的实体?..
更新:只是EF查询的一个例子。
代码:
var query = ctx.EntitySet
.Include("RelatedProperty")
.Where("it.SomeFilter=@param", new ObjectParameter(...));
var sql = query.ToTraceString();
这会产生以下SQL(我编辑了查询以使其更具可读性):
SELECT
[Extent1].[SomeField] AS [SomeField],
/* All remaining Extent1 fields */
[Join1].[Id1] AS [Id1],
/* All remaining Join1 fields */
[Join3].[Id2] AS [Id2]
FROM [OrderSet] AS [Extent1]
LEFT OUTER JOIN (
SELECT
[Extent2].[SomeField] AS [SomeField],
/* All remaining Extent2 fields */
[Extent3].[SomeField] AS [SomeField],
/* All remaining Extent3 fields */
FROM [CustomerSet] AS [Extent2]
LEFT OUTER JOIN [OrderSet] AS [Extent3]
ON [Extent2].[Id] = [Extent3].[Customer_Id] ) AS [Join1]
ON [Extent1].[Customer_Id] = [Join1].[Id1]
LEFT OUTER JOIN (
SELECT
[Extent4].[SomeField] AS [SomeField],
/* All remaining Extent2 fields */
[Extent5].[SomeField] AS [SomeField],
/* All remaining Extent3 fields */
FROM [CustomerSet] AS [Extent4]
LEFT OUTER JOIN [OrderSet] AS [Extent5]
ON [Extent4].[Id] = [Extent5].[Customer_Id] ) AS [Join3]
ON [Extent1].[Customer_Id] = [Join3].[Id4]
WHERE [Extent1].[SomeFilter] = 'blah'
答案 0 :(得分:1)
有没有办法实现绕过EF生成的SQL查询的实体?
在大多数情况下没有因为:
ExceuteStoreQuery
无法加载关系Include
实际选项包括:
where
子句)。如果您已正确配置所有内容并且关闭延迟加载EF将在加载相关实体时自动修复关系