Entity Framework - View Sql with toTraceString

I am trying to view the generated sql that Entity Framework 5.0 generates from an object query. All over the world ( for example, ) everyone says to pass an IQuerable to an ObjectQuery , and then use toTraceString () to return the generated query.

However, I keep getting the exception excluded:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.String]' to type 'System.Data.Objects.ObjectQuery'. 

What is the new way to do this in Entity Framework 5?

+8
logging entity-framework-5 entity-framework
source share
2 answers

You can view the generated SQL from IQueryable using .ToString() , for example.

 var query = context.People.Where(x => x.DomainId == 1); Console.WriteLine(query.ToString()); 
+11
source share

Are you using SQL Server? If so, try using a profiler. Tools-> SQL Server Profiler in Management Studio Development Version

+1
source share

All Articles