I have a C# Entities Framwork 6+ project. I used the following SO post to have the SQL generated for each SQL operation printed to the Output pane:
How do I view the SQL generated by the Entity Framework?
How can I tell if the query is using the available index(es) for the query if a field used in the query has an index for it?
Here is an actual SQL query that was printed to the Output pane:
SQL generated: SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
COUNT(1) AS [A1]
FROM [dbo].[Canonicals] AS [Extent1]
WHERE ([Extent1].[StableID] = @p__linq__0) OR (([Extent1].[StableID] IS NULL) AND (@p__linq__0 IS NULL))
) AS [GroupBy1]
Here is the C# code that generated that SQL. Note the StableID property/field does have an index:
var sqlQuery = this._dbContext.CanonicalSentences.Where((r) => r.StableID.Equals(stableID));
return sqlQuery.Count() > 0;
Here is the declaration of the StableID index using data annotations before the StableID property:
[Index]
[IndexAttribute("StableID", 0, IsUnique = true), StringLength(30)]
public string StableID { get; set; }
FOLLOW-UP: If the SQL shown is not using the StableId index, please tell me how to make Entities Framework (or whatever) generate the correct SQl to use the index.