At 3:15 from the end of this ".NET Core 2.0 Released!" video, Diego Vega shows a demo of new features in Entity Framework Core 2.0. As part of that, a dump of the underlying SQL is shown in the console app.
I have seen many answers on Stack Overflow suggesting that people use an SQL profiler to view the underlying queries. But now I'm curious: how can you do what Diego Vega did, and get the query to show right there in the application?
Update for .NET 6: EF logging is enabled by default in development. See this GitHub issue
Update: Diego added "Microsoft.EntityFrameworkCore.Database.Command": "Information"
to appsettings.Development.json. See How do you show underlying SQL query in EF Core? for more details.
Once the breakpoint is hit hover over query to expand then click on > Debug View > The dropdown beside the magnifying glass> Text Visualizer. There you have it, the SQL that will be sent to the database. Text Visualizer option showing the query generated by EF Core 5.
Visual Studio Output You can set the logging level for Microsoft to “Information”. Then you can view the SQL in the output log when running in debug mode from Visual Studio. The SQL will then be visible in the Output panel.
Hi you can do something like following to display Entity Framework Core generated sql code in output window. In your DbContext
class:
public static readonly Microsoft.Extensions.Logging.LoggerFactory _myLoggerFactory = new LoggerFactory(new[] { new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() }); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseLoggerFactory(_myLoggerFactory); }
The debug logger writes messages in the debug output window only when a debugger is attached.
You will have to do following:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With