0

Based on this and this, I'm doing the following to get the SQL enerated by Entity Framework 5.0

var query = from s in db.ClassesDetails
            where s.ClassSet == "SetOne"
            orderby s.ClassNum
            select s.ClassNum;
var objectQuery = (System.Data.Objects.ObjectQuery)query; // <= problem!
var sql = objectQuery.ToTraceString();

However on the second line I get the following exception:

Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.Int16]' to type 'System.Data.Objects.ObjectQuery'.

Did something change since those SO answers were posted? What do I need to do to get the queries as strings? We're running against Azure SQL so can't run the usual SQL profiler tools :(

Community
  • 1
  • 1
DeepSpace101
  • 13,110
  • 9
  • 77
  • 127

1 Answers1

1

ObjectQuery is created when you are using ObjectContext. When you are using DbContext it uses and creates DbQuery. Also, note that this is actually not a DbQuery but DbQuery<T>. I believe that to display SQL when having DbQueries you can just do .ToString() on the DbQuery instance so no cast should be required. Note that parameter values will not be displayed though. Parameter values were added to the output very recently in EF6 - if you need this you can try the latest nightly build from http://entityframework.codeplex.com

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Pawel
  • 31,342
  • 4
  • 73
  • 104