How do you see the SQL that EF Core generates?

Entity Framework & EF Core Querying Basic 1–2 Yrs2–3 Yrs Debugging 1 min read

Quick Answer

Configure logging in OnConfiguring / AddDbContext: options.LogTo(Console.WriteLine, LogLevel.Information), plus EnableSensitiveDataLogging() in development to see parameter values. For a single query, query.ToQueryString() returns the SQL without running it.

In development I add LogTo(Console.WriteLine, LogLevel.Information) and EnableSensitiveDataLogging to the DbContext options, and then every SQL command EF sends, with parameters and timing, shows up in the console. For a specific query I'm suspicious about, I call .ToQueryString() on the IQueryable to get the SQL without executing it. That's how I catch N+1 problems and queries that are pulling back way more than they should.

  • LogTo(Console.WriteLine) or route through ILogger — you see every command, its parameters (if sensitive logging is on) and how long it took.
  • ToQueryString() — inspect the SQL for a specific IQueryable while debugging.
  • Watch for: N+1 bursts, SELECTing far more columns than needed, client-evaluation warnings, missing WHERE pushed to the database.
  • In production use a proper sink (Seq, App Insights) at Warning+ and only turn on command logging temporarily.