Home >Database >Mysql Tutorial >How Can I Log the SQL Generated by DbContext.SaveChanges()?
Logging the Generated SQL from DbContext.SaveChanges() in Your Program
Question:
How can you capture and log the SQL statements generated by DbContext.SaveChanges()?
Answer:
In Entity Framework 6.0:
context.Database.Log = Console.WriteLine;
For Advanced Scenarios:
For more complex logging requirements, consider using an interceptor. Interceptors allow you to customize how the DbContext interacts with the database. By implementing a custom interceptor, you can log the generated SQL statements and perform other actions before or after executing commands.
The above is the detailed content of How Can I Log the SQL Generated by DbContext.SaveChanges()?. For more information, please follow other related articles on the PHP Chinese website!