Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exact sql query executed by Entity Framework

the question seems clear enough, but I'll add a case

using (var context = new MyEntities())
{
  if(context.mytable.Any(row => row.myfield == 2))
  {
    // do something here
  }
}

I am new to Entity Framework. I don't know how to check the exact sql query executed?

like image 475
Anwar Chandra Avatar asked Feb 21 '26 07:02

Anwar Chandra


1 Answers

As the above answers state, you can use SQL Profiler, LINQPad, EF Profiler, etc.

Another little known (some might say lazy) trick is to use ObjectQuery.ToTraceString() extension method.

Just cast your query as ObjectQuery<T>.

var query = context.mytable.Any(row => row.myfield == 2));
var trace = ((ObjectQuery<MyTable>)query).ToTraceString();

It will spit out the SQL that is to be executed.

Very handy for last-minute logging.

like image 67
RPM1984 Avatar answered Feb 24 '26 05:02

RPM1984



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!