当使用实体框架ctp5“只使用代码”时,如何获得LINQ查询下的原始SQL ?

时间:2021-11-08 09:41:59

I've using Entity Framework CTP5 in "code only" mode. I'm running a LINQ query on a object that was return from the database, as the query is running really slowly. Is there any way in which I can get the SQL statement that is being generated from the query?

我在“只使用代码”模式中使用了实体框架CTP5。我正在对从数据库返回的对象运行LINQ查询,因为查询运行得很慢。有什么方法可以让我获得从查询生成的SQL语句吗?

Topic currentTopic =
    (from x in Repository.Topics
     let isCurrent = (x.StoppedAt <= x.StartedAt || (x.StartedAt >= currentTopicsStartedAtOrAfter))
     where x.Meeting.Manager.User.Id == user.Id && isCurrent
     orderby x.StartedAt descending
     select x).FirstOrDefault();

The "Repository" property is a descendent of DbContext.

“存储库”属性是DbContext的后代。

It's a little complicated, as EF can't use my helper methods on the objects, so I'm specifying the logic directly in the query.

这有点复杂,因为EF不能在对象上使用helper方法,所以我直接在查询中指定逻辑。

So, is there any way I can dump the SQL that will be produced by that LINQ query (e.g. to my log4net repository)?

那么,是否有任何方法可以转储由LINQ查询生成的SQL(例如,将其转储到我的log4net存储库中)?

5 个解决方案

#1


3  

I'd either use SQL Trace to grab the query running on the server directly, or use the Event Tracing for Windows (SQL Profiling) feature out of ANTS Performance Profiler.

我要么使用SQL跟踪直接获取在服务器上运行的查询,要么使用ant性能分析器之外的Windows (SQL Profiling)特性的事件跟踪。

#2


19  

You can try using Entity Framework tracing provider as described here (but it is old post for CTP3).

您可以尝试使用这里描述的实体框架跟踪提供程序(但它是CTP3的旧版本)。

Your other choices are:

你其他的选择是:

  • SQL Server Profiler - part of MS SQL Developer tools (in case your DB is SQL Server)
  • SQL Server Profiler—MS SQL开发工具的一部分(如果您的DB是SQL Server)
  • Intelli trace - only in VS 2010 Ultimate but it doesn't show parameter values
  • Intelli跟踪-仅在VS 2010终极,但它不显示参数值
  • Hugati Query Profiler
  • Hugati查询分析器
  • Entity framework profiler
  • 实体框架分析器

In common EF you can also use ToTraceString as @Andy suggested but DbQuery in CodeFirst doesn't have this method (or I didn't find it).

在普通的EF中,也可以像@Andy建议的那样使用ToTraceString,但是CodeFirst中的DbQuery没有这个方法(或者我没有找到)。

Edit:

编辑:

So DbQuery doesn't have ToTraceString because it is directly implemented as ToString.

因此,DbQuery不具有ToTraceString,因为它直接实现为ToString。

#3


9  

This worked for me and it is free:

这对我有用,而且是免费的:

public static class DebugExtensions
{
    private static object GetPropertyValue(object o, string Name)
    {
        return o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(x => x.Name == Name).First().GetValue(o, null);
    }
    public static string ToTraceString(this IQueryable query)
    {
        var oquery = (ObjectQuery)GetPropertyValue(GetPropertyValue(query, "InternalQuery"), "ObjectQuery");
        return oquery.ToTraceString();
    }
}

Usage:

用法:

   var rows = db.Forecasts.Take(1);
   System.Diagnostics.Debug.WriteLine(rows.ToTraceString());

#4


1  

The extension method ToTraceString() might be what you're looking for:

扩展方法ToTraceString()可能是您正在寻找的:

http://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx

http://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx

#5


1  

Setting up logging is as easy as:

设置日志记录很简单:

context.Database.Log = Console.WriteLine;

Original answer: https://*.com/a/20757916/2183503

最初的回答:https://*.com/a/20757916/2183503

#1


3  

I'd either use SQL Trace to grab the query running on the server directly, or use the Event Tracing for Windows (SQL Profiling) feature out of ANTS Performance Profiler.

我要么使用SQL跟踪直接获取在服务器上运行的查询,要么使用ant性能分析器之外的Windows (SQL Profiling)特性的事件跟踪。

#2


19  

You can try using Entity Framework tracing provider as described here (but it is old post for CTP3).

您可以尝试使用这里描述的实体框架跟踪提供程序(但它是CTP3的旧版本)。

Your other choices are:

你其他的选择是:

  • SQL Server Profiler - part of MS SQL Developer tools (in case your DB is SQL Server)
  • SQL Server Profiler—MS SQL开发工具的一部分(如果您的DB是SQL Server)
  • Intelli trace - only in VS 2010 Ultimate but it doesn't show parameter values
  • Intelli跟踪-仅在VS 2010终极,但它不显示参数值
  • Hugati Query Profiler
  • Hugati查询分析器
  • Entity framework profiler
  • 实体框架分析器

In common EF you can also use ToTraceString as @Andy suggested but DbQuery in CodeFirst doesn't have this method (or I didn't find it).

在普通的EF中,也可以像@Andy建议的那样使用ToTraceString,但是CodeFirst中的DbQuery没有这个方法(或者我没有找到)。

Edit:

编辑:

So DbQuery doesn't have ToTraceString because it is directly implemented as ToString.

因此,DbQuery不具有ToTraceString,因为它直接实现为ToString。

#3


9  

This worked for me and it is free:

这对我有用,而且是免费的:

public static class DebugExtensions
{
    private static object GetPropertyValue(object o, string Name)
    {
        return o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(x => x.Name == Name).First().GetValue(o, null);
    }
    public static string ToTraceString(this IQueryable query)
    {
        var oquery = (ObjectQuery)GetPropertyValue(GetPropertyValue(query, "InternalQuery"), "ObjectQuery");
        return oquery.ToTraceString();
    }
}

Usage:

用法:

   var rows = db.Forecasts.Take(1);
   System.Diagnostics.Debug.WriteLine(rows.ToTraceString());

#4


1  

The extension method ToTraceString() might be what you're looking for:

扩展方法ToTraceString()可能是您正在寻找的:

http://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx

http://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx

#5


1  

Setting up logging is as easy as:

设置日志记录很简单:

context.Database.Log = Console.WriteLine;

Original answer: https://*.com/a/20757916/2183503

最初的回答:https://*.com/a/20757916/2183503