How can I log the generated SQL from DbContext.SaveChanges() in my Program?

Masoud picture Masoud · Jun 2, 2013 · Viewed 71.9k times · Source

According this thread, we can log the generated SQL via EF, but what about DbContext.SaveChanges()? Is there any easy way to do this job without any extra frameworks?

Answer

Lorentz Vedeler picture Lorentz Vedeler · Dec 24, 2013

In entity framework 6.0, the Database class has a property Action<string> Log. so setting up logging is as easy as:

context.Database.Log = Console.WriteLine;

For more advanced needs you can set up an interceptor.