Posts Tagged ‘trace’

How do I view the SQL generated by the entity framework?

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

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

Source: http://stackoverflow.com/questions/1412863/how-do-i-view-the-sql-generated-by-the-entity-framework

I create a extension…

public static class IQueryableExtension
{
     public static String ToTraceSQL(this IQueryable obj) 
     {             
         return ((System.Data.Objects.ObjectQuery)obj).ToTraceString();
     }
}

Using this:

var iQuerableResult = dataBase.BiServico.Take(10);
string myResustSQL = iQuerableResult.ToTraceSQL();

Shazan! =)

hope I have helped!