我在应用程序中使用Entity Framework 4.0如何打印sql语句的日志.在EF 6 sampleEntities.Database.Log工作如下
readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public sampleEntities()
: base("name=sampleEntities")
{
this.Database.Log = s => log.Info("LINQSQLLOG : " + s);
}
解决方法:
您可以如下所示进行操作.
方法1:
IQueryable myQuery = from x in yourEntities
where y.id = 45
select y;
var sql = ((System.Data.Objects.ObjectQuery)myQuery).ToTraceString();
方法2:
您可以使用Clutch.Diagnostics.EntityFramework API.它提供用于跟踪EntityFramework sql命令的API.
努吉特:
PM > Install-Package Clutch.Diagnostics.EntityFramework
Git上的API:Clutch API
更新:Logging and Tracing SQL Queries Clutch