Entity framework 6 未来正在生成重复查询

Entity framework 6 future is generating duplicate queries

以下代码写在entity framework6和entity framework plus库(futures)

using (var ctx = _contextCreator.PortalContext())
{
    var q = "";
    ctx.Database.Log = s => q+=s;
    
    var a = ctx.ArticleIcons.Take(1).Future();
    var b = ctx.ArticleIcons.Take(3).Future();
    
    var aResult = a.ToList();
}

这是“q”变量的输出,它输出生成的 SQL 查询。是否可以避免执行4个查询,因为代码中只有2个查询?

SELECT TOP (1) 
    [c].[Id] AS [Id], 
    [c].[Name] AS [Name], 
    [c].[CssClass] AS [CssClass]
    FROM [dbo].[ArticleIcons] AS [c]
-- Executing at 25.3.2022. 9:41:47 +01:00
-- Completed in 29 ms with result: null

SELECT TOP (3) 
    [c].[Id] AS [Id], 
    [c].[Name] AS [Name], 
    [c].[CssClass] AS [CssClass]
    FROM [dbo].[ArticleIcons] AS [c]
-- Executing at 25.3.2022. 9:41:47 +01:00
-- Completed in 0 ms with result: null

Opened connection at 25.3.2022. 9:41:48 +01:00
-- EF+ Query Future: 1 of 2
SELECT TOP (1) 
    [c].[Id] AS [Id], 
    [c].[Name] AS [Name], 
    [c].[CssClass] AS [CssClass]
    FROM [dbo].[ArticleIcons] AS [c]
;

-- EF+ Query Future: 2 of 2
SELECT TOP (3) 
    [c].[Id] AS [Id], 
    [c].[Name] AS [Name], 
    [c].[CssClass] AS [CssClass]
    FROM [dbo].[ArticleIcons] AS [c]
;

-- Executing at 25.3.2022. 9:41:48 +01:00
-- Completed in 53 ms with result: SqlDataReader

Closed connection at 25.3.2022. 9:41:48 +01:00

查询被记录 2 次,原因如下:

第一次,库生成两个查询(不执行它们,只生成它们)。但是,要正确生成它,它必须调用记录这两个查询的 interceptor ReaderExecuting 和 ReaderExecuting。此步骤是强制性的,因为某些提供程序(例如 MySql/自定义用户代码更改查询和参数。

第二次,合并查询后,库调用 DbInterception.Dispatch.Command.Reader 拦截器记录合并查询。

因此,由于限制,查询被记录了 2 次,在数据库服务器中只执行了组合查询。