exact sql query executed by Entity Framework

后端 未结 3 1430
忘掉有多难
忘掉有多难 2021-01-23 05:25

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

using (var context = new MyEntities())
{
  if(context.mytable.Any(row => row.myfield == 2))
  {
    // d         


        
3条回答
  •  半阙折子戏
    2021-01-23 05:47

    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.

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

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

    Very handy for last-minute logging.

提交回复
热议问题