views:

745

answers:

2

How do I view the SQL generated by entity framework ?

(In my particular case I'm using the mysql provider - if it matters)

Duplicate: http://stackoverflow.com/questions/137712/sql-tracing-linq-to-entities#138109

+1  A: 

There are two ways:

  1. To view the SQL that will be generated, simply call ToTraceString(). You can add it into your watch window and set a breakpoint to see what the query would be at any given point for any LINQ query.
  2. You can attach a tracer to your SQL server of choice, which will show you the final query in all its gory detail. In the case of MySQL, the easiest way to trace the queries is simply to tail the query log with tail -f. You can learn more about MySQL's logging facilities in the official documentation. For SQL Server, the easiest way is to use the included SQL Server profiler.
Benjamin Pollack
The ToTraceString of what ?
nos
The ObjectQuery, as Nick noted right after I posted my response.
Benjamin Pollack
+7  A: 

You can do the following:

var result = from x in appEntities
             where x.id = 32
             select x;

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

That will give you the SQL that was generated.

Nick Berardi
It works with MySQL too.
Nick Berardi

related questions