How to Count Rows with CountAsync in EF Core
CountAsync asks MySQL how many rows match, without loading any of them. It's what you need for totals, such as "page 2 of 7". This guide shows its forms and the SQL each one sends.
CountAsync asks MySQL how many rows match, without loading any of them. It's what you need for totals, such as "page 2 of 7". This guide shows its forms and the SQL each one sends.
A product belongs to a category, but a query for products doesn't load the category unless you ask. There are two ways to ask: Include and Select. They both produce a join and differ in how much they load. This guide runs each one on MySQL and reads the SQL, including what happens if you ask for neither.
By default, EF Core remembers every entity a query returns, in case you change it. AsNoTracking turns that off for one query, which saves work when you only want to read. This guide shows what it changes and what it doesn't, measures the difference on 5,000 rows, and explains the cases where you don't need it at all.
When several queries return the same response, you want one definition of how to build it. The obvious way, a normal C# method inside Select, loads every column, and throws NullReferenceException as soon as it touches a navigation. The same code written as an Expression lets EF Core read it, and it sends only the columns and joins it needs.
Reading a whole entity sends every column over the wire, even when you only use two. A Select call tells EF Core which values you want, and it asks MySQL for exactly those. This guide compares the SQL for both, maps the result to a record, and checks what happens to change tracking.
EF Core has three common ways to read one row: FirstOrDefaultAsync, SingleOrDefaultAsync and FindAsync. They look the same on a happy day and differ in what they send to MySQL and when they throw. This guide runs all three on the same row and compares the SQL, so you can pick with evidence.
With EF Core, you read rows by writing a LINQ query in C#, and EF Core turns it into SQL. The part that surprises people is timing: building the query sends nothing, and only ToListAsync or a similar call goes to MySQL. This guide reads rows with Where and OrderBy, shows the SQL each piece becomes, and proves the timing with real logs.
AddRange adds a list of new objects to the change tracker, and one SaveChangesAsync inserts them all. With Oracle's MySQL provider, EF Core packs the INSERT statements into one command to save round trips, and every object gets its own ID. This guide shows the code and the SQL it sends for 3 rows and for 50.
To insert a row with EF Core, add an object to its DbSet and call SaveChangesAsync. When that returns, the object's Id holds the number MySQL gave it, so you never have to query for it. This guide shows the two lines, the SQL they send, and how that new ID finds its way back.
ASP.NET Core can describe your API as an OpenAPI document, but that's a JSON file meant for tools. Scalar turns it into a page you can read and try in a browser. It takes one package and one line of code. This guide adds it to a .NET 10 C# Web API, for Development only.