.NET Developer Interview Question

An endpoint using EF Core is slow and runs hundreds of queries per request. How do you fix it?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Confirm it by logging the generated SQL or using a profiler, then look for the N+1 pattern: one query for a list, then another for each item's related data, usually from lazy loading or from querying inside a loop. Fix it by projecting exactly the data the endpoint needs with Select, or eager loading with Include, and consider split queries when several collections are included. Then add a check so it cannot quietly return.

Why interviewers ask this

N+1 queries are the most common EF Core performance problem, and they lurk inside clean looking code. The interviewer wants a diagnosis first, through SQL logging or a profiler, then the right fix for the situation. Knowing that projection usually beats Include for read endpoints, and that including several collections can cause a cartesian explosion, separates people who have tuned EF Core from people who have only used it.

How to structure your answer

  • Confirm the query pattern with logging or a profiler.
  • Identify the cause: lazy loading or queries inside a loop.
  • Fix with projection, eager loading or split queries.
  • Prevent regressions with a query count check.

Example answer

Spoken example, first person

First I want to see the SQL, from EF Core logging locally or from our traces. Hundreds of near identical queries with different ids is the N+1 signature: one query loads the orders, then each order triggers another query for its lines or customer. The cause is usually lazy loading proxies, or a loop in a service that calls the repository once per item. For a read endpoint my preferred fix is projection. A Select into a response model naming exactly the fields we return becomes a single query with the joins EF Core needs, and it only fetches the columns used. If I need full entities, for example to modify them, I use Include and ThenInclude. When an Include pulls in two or three collections, the joined result can multiply in size, so I make that a split query, which runs one query per collection instead. On one endpoint, projection took it from over two hundred queries to one, and response time fell from seconds to well under a hundred milliseconds. Then I added an integration test that counts its queries.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • When would you disable lazy loading entirely?
  • What is a cartesian explosion, and how do split queries help?
  • How would you catch N+1 problems before they reach production?

Related .net developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot