<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dmytro</title>
    <description>The latest articles on DEV Community by Dmytro (@kiwidevelopment).</description>
    <link>https://dev.to/kiwidevelopment</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3875576%2Fb7e095a8-10da-4b37-aa2a-2155d5aa6b80.png</url>
      <title>DEV Community: Dmytro</title>
      <link>https://dev.to/kiwidevelopment</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kiwidevelopment"/>
    <language>en</language>
    <item>
      <title>How to catch N+1 queries in EF Core before they hit production</title>
      <dc:creator>Dmytro</dc:creator>
      <pubDate>Sun, 12 Apr 2026 22:45:30 +0000</pubDate>
      <link>https://dev.to/kiwidevelopment/how-to-catch-n1-queries-in-ef-core-before-they-hit-production-4g7j</link>
      <guid>https://dev.to/kiwidevelopment/how-to-catch-n1-queries-in-ef-core-before-they-hit-production-4g7j</guid>
      <description>&lt;p&gt;Your API can stay functionally correct while quietly getting slower.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No tests fail.
&lt;/li&gt;
&lt;li&gt;No alerts fire.
&lt;/li&gt;
&lt;li&gt;Everything “works”.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;And then one day you realize an endpoint that used to execute 2 queries is now doing 15.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In EF Core, it’s surprisingly easy to introduce query regressions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a small refactor changes how a projection is built&lt;/li&gt;
&lt;li&gt;a navigation property is accessed differently&lt;/li&gt;
&lt;li&gt;part of the query gets materialized too early&lt;/li&gt;
&lt;li&gt;includes / relationships evolve over time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of this breaks correctness. Your integration tests still pass because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the response is correct&lt;/li&gt;
&lt;li&gt;the database state is correct&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;But the query shape has changed. And that’s the part we usually don’t test.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why this matters ? This isn’t about premature optimization. It’s about catching issues like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;N+1 queries&lt;/li&gt;
&lt;li&gt;unnecessary roundtrips&lt;/li&gt;
&lt;li&gt;query explosions after refactors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These problems don’t show up as failing tests — they show up as slow endpoints in production. If you want to detect this in EF Core tests, you typically end up doing something like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;writing a custom &lt;code&gt;DbCommandInterceptor&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;wiring it into your test host&lt;/li&gt;
&lt;li&gt;collecting executed SQL&lt;/li&gt;
&lt;li&gt;asserting on the count
It works, but it’s repetitive and low-level.
Most teams end up copy-pasting some version of this.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A simpler approach
&lt;/h2&gt;

&lt;p&gt;I wanted something closer to what Django has with &lt;code&gt;assertNumQueries&lt;/code&gt;. So I wrapped the interceptor pattern into a small helper.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;guard&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TrackQueries&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Program&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;AppDbContext&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/api/orders"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AssertCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;atMost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;no manual interceptor wiring&lt;/li&gt;
&lt;li&gt;no log parsing&lt;/li&gt;
&lt;li&gt;no test boilerplate
It just tracks the number of SQL queries executed during a request.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to use this
&lt;/h2&gt;

&lt;p&gt;I don’t think this belongs in every test. For most endpoints, correctness is enough. But it’s useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;list endpoints&lt;/li&gt;
&lt;li&gt;dashboards / aggregates&lt;/li&gt;
&lt;li&gt;endpoints with multiple relationships&lt;/li&gt;
&lt;li&gt;any “hot path” where query shape matters
Basically, places where going from 2 queries → 10 queries is a real problem.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The idea
&lt;/h2&gt;

&lt;p&gt;This isn’t about replacing integration tests. It’s about adding a lightweight guard against performance regressions. Because those are the bugs that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;don’t break functionality&lt;/li&gt;
&lt;li&gt;don’t fail tests&lt;/li&gt;
&lt;li&gt;but hurt you in production&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re curious,I put the helper here:&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/KiwiDevelopment/KiwiQuery" rel="noopener noreferrer"&gt;https://github.com/KiwiDevelopment/KiwiQuery&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s very small (~200 lines), MIT licensed.&lt;br&gt;
Would be interested to know how others are solving this — or if you’re not testing for it at all.&lt;/p&gt;

</description>
      <category>database</category>
      <category>dotnet</category>
      <category>performance</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
