Anwar Chandra
Anwar Chandra

Reputation: 8638

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?

Upvotes: 2

Views: 5228

Answers (3)

RPM1984
RPM1984

Reputation: 73112

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.

Upvotes: 8

Yakimych
Yakimych

Reputation: 17752

You can find a similar question here: How to view generated SQL from Entity Framework?

To sum up, your choices are:

See the referenced question for details.

Upvotes: 4

Neil Knight
Neil Knight

Reputation: 48547

If you are connecting to SQL Server, you can use the SQL Profiler in order to obtain the SQL that is generated.

Upvotes: 0

Related Questions