Pro EP 23 : The life of Query in Entity Framework (EF)

Muhammad Waseem
Become .NET Pro !
2 min readMar 14, 2023

𝐒𝐭𝐞𝐩 𝟏 : The LINQ Query is processed by EF Core and build an representation that is processed by database provider, and the result is cached later on so we don’t need to process it every time.

𝐒𝐭𝐞𝐩 𝟐 : The result is passed to the database provider and database provider identifies which parts of query can be evaluated in database, these parts are then translated into query language (e.g. SQL) after that translated query is sent to database and database returns results (but not entity instances)

𝐒𝐭𝐞𝐩 𝟑 : For each item in the result we first check is this tracking query or not , if its tracking query then EF checks the data in existing change tracker if found relevant entity is returned otherwise new entity is created , its change tracking get set up and its returned.
For non tracking query a new entity is always created and returned.

𝐖𝐡𝐞𝐧 𝐪𝐮𝐞𝐫𝐢𝐞𝐬 𝐚𝐫𝐞 𝐞𝐱𝐞𝐜𝐮𝐭𝐞𝐝
When you write a LINQ query it build in memory representation until you call following to retrieve results.

✔️Iterating the results in a for loop

✔️Using an operator such as ToList, ToArray, Single, Count, or the equivalent async overloads.

If you want to help the author in growing

  1. Subscribe my YouTube Channel where I would be sharing videos on .NET
  2. Subscribe my Weekly .NET Newsletter of C#/.NET
  3. Download my eBook at Gum road that contains 30+ .NET Tips.
  4. Become Patron to get access to 80+ Compiled Questions and Answers at one place

--

--