liveBook · Manning

SQL

As explained in Pro ASP.NET Core 7, Tenth Edition

SQL is a standardized language for defining, querying, manipulating, and managing data in relational databases.

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

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Shows translated SELECT and INSERT commands and their logs, but does not teach SQL syntax from scratch.
Examples
Uses a calculation endpoint’s SELECT and INSERT logs and a Blazor component that issues duplicate SELECT commands.
What it assumes
Assumes a working understanding of SQL and uses C# LINQ rather than teaching SQL from zero.
What you can do after
Diagnose/debug with it by inspecting generated SQL logs for inefficient lookups or queries repeated during rendering.
How it connects
Connects LINQ translation and Entity Framework Core performance to database command logging, caching, and Blazor rerendering.

Also explained in

Compare how 25 books explain SQL →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.