我注意到实体框架在使用该Find()
方法时会生成一些低效的查询。例如,这是我的 C# 代码。
Model model = unit.Repository.DbSet.Find(model.ID);
生成 Find() 查询
DECLARE @p0 int = 1
SELECT
[Limit1].[ID] AS [ID],
[Limit1].[UserID] AS [UserID],
[Limit1].[Started] AS [Started],
[Limit1].[Updated] AS [Updated],
[Limit1].[Completed] AS [Completed]
FROM ( SELECT TOP (2)
[Extent1].[ID] AS [ID],
[Extent1].[UserID] AS [UserID],
[Extent1].[Started] AS [Started],
[Extent1].[Updated] AS [Updated],
[Extent1].[Completed] AS [Completed]
FROM [dbo].[Table] AS [Extent1]
WHERE [Extent1].[ID] = @p0
) AS [Limit1]
它似乎正在运行另一个不必要的选择查询。这是使用该SingleOrDefault()
方法的输出。
生成 SingleOrDefault() 查询
DECLARE @p__linq__0 int = 1
SELECT TOP (2)
[Extent1].[ID] AS [ID],
[Extent1].[UserID] AS [UserID],
[Extent1].[Started] AS [Started],
[Extent1].[Updated] AS [Updated],
[Extent1].[Completed] AS [Completed]
FROM [dbo].[Table] AS [Extent1]
WHERE [Extent1].[ID] = @p__linq__0
Find()
生成两个选择是否有原因?是否应该Find()
避免该方法以支持该SingleOrDefault()
方法?