实体框架4.0:如何记录SQL语句

4

我在应用程序中使用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个回答

4
你可以按照以下步骤进行操作。 方法一:
IQueryable myQuery = from x in yourEntities
             where y.id = 45 
             select y;

var sql = ((System.Data.Objects.ObjectQuery)myQuery).ToTraceString();

方法二:

你可以使用 Clutch.Diagnostics.EntityFramework API,该API提供了用于跟踪EntityFramework SQL命令的API。

Nuget:

PM > Install-Package Clutch.Diagnostics.EntityFramework

Git上的API:Clutch API

更新:使用Logging and Tracing SQL Queries Clutch


谢谢回复,我会尝试第二个方法。是否可以使用Clutch.Diagnostics.EntityFramework记录所有SQL语句,因为有些结果像下面这样列出:var Reminder = context.Reminders.ToList();? - user202

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接