Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get generated SQL for a DbContext.SaveChanges in Entity Framework Core

In Entity Framework Core, is it possible to see the SQL that will be applied when the SaveChanges() method is called on the DbContext?

like image 888
Mike Coxeter Avatar asked May 26 '19 05:05

Mike Coxeter


1 Answers

EF Core 5.0+

There are a simple builtin solution, add the following function to the DbContext class.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.LogTo(Console.WriteLine);

this will log both queries and commands.

See here for mote details: Simple Logging

like image 100
IFink Avatar answered Jan 01 '23 10:01

IFink