SQL is the language sent to a relational database to retrieve and store data. In an ASP.NET Core application using Entity Framework Core, endpoint code can express a query with LINQ; Entity Framework Core translates it into SQL and turns returned rows into data model objects. That translation is worth watching: treating Entity Framework Core as a black box can hide queries that retrieve far more data than the application needs. section 17.5 section 17.5.6
A calculation endpoint
An endpoint looks up a stored calculation with FirstOrDefault. If none is found, it calculates the result, adds a new object, and calls SaveChangesAsync. The lookup produces a SELECT; saving produces an INSERT. Requesting /sum/10 shows the seeded result, while /sum/100 triggers a calculation and storage. Repeating a request still sends a query: the retrieved value is not cached. section 17.5.6
...
Executing DbCommand [Parameters=[@__count_0='?' (DbType = Int32)],
CommandType='Text', CommandTimeout='30']
SELECT TOP(1) [c].[Id], [c].[Count], [c].[Result]
FROM [Calculations] AS [c]
WHERE [c].[Count] = @__count_0
...
The command log exposes the generated SELECT, including TOP(1) and the parameter used to find the requested calculation. section 17.5.6
A LINQ query held in a property is not a stored set of results. Reading the property can execute the SQL again. In a Blazor component, Count() and @foreach each read the same property; a rerender caused by an unrelated button click repeats both queries. Check the database command log when a page appears to query more often than its data operations suggest. section 36.3.2
... info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [p].[PersonId], [p].[DepartmentId], [p].[Firstname], [p].[LocationId], [p].[Surname], [d].[Departmentid], [d].[Name], [l].[LocationId], [l].[City], [l].[State] FROM [People] AS [p] INNER JOIN [Departments] AS [d] ON [p].[DepartmentId] = [d].[Departmentid] INNER JOIN [Locations] AS [l] ON [p].[LocationId] = [l].[LocationId] info: Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [p].[PersonId], [p].[DepartmentId], [p].[Firstname], [p].[LocationId], [p].[Surname], [d].[Departmentid], [d].[Name], [l].[LocationId], [l].[City], [l].[State] FROM [People] AS [p] INNER JOIN [Departments] AS [d] ON [p].[DepartmentId] = [d].[Departmentid] INNER JOIN [Locations] AS [l] ON [p].[LocationId] = [l].[LocationId] ...
Two matching SELECT commands make the repeated-query problem visible in the server log. section 36.3.2
























