简体   繁体   中英

Entity Framework - View Sql using toTraceString

I'm trying to view the generated sql that Entity Framework 5.0 generates from an entities query. All over the web, everyone says to cast the IQuerable object to an ObjectQuery object and then use the toTraceString() method to return the generated query.

However I keep getting an invalid case exception:

    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?

You can view the generated SQL from an IQueryable using .ToString() , eg

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

Are you using SQL Server? If so, try using the profiler. Tools->SQL Server Profiler in the development version of Management Studio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM