Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 533
Rafi Avatar asked Apr 14 '13 18:04

Rafi


People also ask

Can we use SQL query in Entity Framework?

Entity Framework allows you to execute raw SQL queries for the underlying relational database.


1 Answers

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

var query = context.People.Where(x => x.DomainId == 1);
Console.WriteLine(query.ToString());
like image 75
Richard Avatar answered Oct 06 '22 23:10

Richard