Reputation: 10624
I'm using Entity Framework4 (Code First).
and I want to see the generated query.
I found this code in Stackoverflow.
var trace = ((System.Data.Objects.ObjectQuery)result).ToTraceString();
but this is not working, the error message say,
Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[PJ.Mysql.Entities.Order]' to type 'System.Data.Objects.ObjectQuery'.
My code is,
[HttpGet]
public void test()
{
EFOrdersRepository ordersRepository = new EFOrdersRepository();
var query = ordersRepository.Orders;
var result = from x in query
where x.orderid == 99008326
select x;
var trace = ((System.Data.Objects.ObjectQuery)result).ToTraceString(); //it does not work
Response.Write(trace);
}
EFOrdersRepository.cs
public class EFOrdersRepository
{
private EFMysqlContext context = new EFMysqlContext();
public IQueryable<Order> Orders
{
get { return context.orders; }
}
}
EFMysqlContext.cs
class EFMysqlContext : DbContext
{
public DbSet<Order> orders { get; set; }
}
I really want to solve this problem =3, anybody knows ?
Upvotes: 0
Views: 870
Reputation: 1038800
Checkout the mini-profiler which allows you to trace SQL queries. And here's a nice blog post detailing its setup.
Upvotes: 2