exact sql query executed by Entity Framework

家住魔仙堡 提交于 2020-01-11 10:41:49

问题


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?


回答1:


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

To sum up, your choices are:

  • LINQPad
  • SQL Server Profiler
  • Entity Framework Profiler

See the referenced question for details.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4385756/exact-sql-query-executed-by-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!