我一直在研究EF6生成的一些SQL,并且不确定为什么以下C#不会生成更简单的东西。
var db = new QueryContext();
db.Database.Log = (s) => Console.WriteLine(s);
var query = db.Set<Books>().AsNoTracking().AsQueryable();
query.Count();
对于针对SQL Server 2016的查询,生成的查询看起来像这样:
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Books] AS [Extent1]
) AS [GroupBy1]
我想捕获生成的SQL并显示给用户。如果它回来我会很高兴:
SELECT COUNT(1) AS [A1]
FROM [dbo].[Books]