实体框架DbContext执行的日志查询 [英] Log Queries executed by Entity Framework DbContext
问题描述
在Java / Hibernate中,可以通过设置属性 hibernate.show_sql = true
。在Entity Framework中可能有类似的行为?
记录和拦截数据库操作 MSDN上的文章是您正在寻找的。 p>
可以将任何方法的 DbContext.Database.Log
属性设置为委托。最常见的是通过将其设置为该TextWriter的Write方法与任何 TextWriter
一起使用。当前上下文生成的所有SQL都将被记录到该作者。例如,以下代码将SQL记录到控制台:
using(var context = new BlogContext())
{
context.Database.Log = Console.Write;
//您的代码在这里...
}
I'm using EF 6.0 with LINQ in MVC 5 project. I want to log all the SQL queries executed by the Entity Framework DbContext for debugging/performance-measurement purpose.
In Java/Hibernate, equivalent behavior can be achieved by setting the property hibernate.show_sql=true
. Is it possible to have a similar behavior in Entity Framework?
Logging and Intercepting Database Operations article at MSDN is what your are looking for.
The DbContext.Database.Log
property can be set to a delegate for any method that takes a string. Most commonly it is used with any TextWriter
by setting it to the "Write" method of that TextWriter. All SQL generated by the current context will be logged to that writer. For example, the following code will log SQL to the console:
using (var context = new BlogContext())
{
context.Database.Log = Console.Write;
// Your code here...
}
这篇关于实体框架DbContext执行的日志查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!