View SQL queries run by Entity Framework Core in the console

In Entity Framework Core writing the executed queries to the console is easy. Just create a ILoggerFactory and then configure EF Core to use it in the OnConfiguring method.

The code below is valid as of Entity Framework Core 3.1.3.

Output EF Core queries to the console

Remember if your using a database first approach your OnConfiguring customisations will be erased when you re-scaffold your DB so put your customisations in a subclass of the EF generated context.

*** Note ***
In EF Core 5 you’ll be able exclude OnConfiguring when scaffolding.

Leave a Reply

Your email address will not be published. Required fields are marked *