I use EF 5 for my project and im profilin it with EntityProfiler and i see that sql that is generated whene i use Find for the query is:
SELECT TOP (2) [Extent1].[ID] AS [ID],
[Extent1].[TargetID] AS [TargetID],
[Extent1].[BranchID] AS [BranchID],
[Extent1].[ApplicationStatus] AS [ApplicationStatus],
[Extent1].[UserID] AS [UserID],
[Extent1].[AssignedOfficer] AS [AssignedOfficer],
[Extent1].[AssignedOfficerCRM] AS [AssignedOfficerCRM],
[Extent1].[RegistrationDate] AS [RegistrationDate],
[Extent1].[DecisionReasons] AS [DecisionReasons],
[Extent1].[DecisionExceptionID] AS [DecisionExceptionID],
[Extent1].[RiskComment] AS [RiskComment],
[Extent1].[CESInformed] AS [CESInformed],
[Extent1].[IsCommited] AS [IsCommited]
FROM [dbo].[Applications] AS [Extent1]
WHERE [Extent1].[ID] = '900100' /* @p0 */
Code that is called is:
public T GetByID(object primaryKey)
{
return DB.Set<T>().Find(primaryKey);
}
so my questin is why in sql that is generated is Select Top (2)
It executes
Select Top (2)cause the DBSet uses internallySingleOrDefault()(see here methodFindInStore) for theFindmethod to execute the query.This ensures, that if 2 results are returned, an exception is thrown, cause SingleOrDefault defines that it expects exactly one result or nothing.
Select Top (1)is generated as Sql wenn you use FirstOrDefault().