How do you see the SQL that EF Core generates?
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 throughILogger— you see every command, its parameters (if sensitive logging is on) and how long it took.ToQueryString()— inspect the SQL for a specificIQueryablewhile debugging.- Watch for: N+1 bursts,
SELECTing far more columns than needed, client-evaluation warnings, missingWHEREpushed to the database. - In production use a proper sink (Seq, App Insights) at
Warning+ and only turn on command logging temporarily.