entity-frameworkado.net

How do I view the SQL generated by the Entity Framework?


How do I view the SQL generated by entity framework ?

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


Solution

  • You can do the following:

    IQueryable query = from x in appEntities
                 where x.id == 32
                 select x;
    
    var sql = ((System.Data.Objects.ObjectQuery)query).ToTraceString();
    

    or in EF6:

    var sql = ((System.Data.Entity.Core.Objects.ObjectQuery)query)
                .ToTraceString();
    

    or in EF6.3+:

    var sql = ((dynamic)flooringStoresProducts).Sql;
    

    That will give you the SQL that was generated.