老版本中:EF 4和EF 3.5 SP1
using (var context = new TestDBEntities()) { var query = from p in context.Parents where p.Name == "Lingzhi" select p; ObjectQuery<Parent> parents = query as ObjectQuery<Parent>; if (parents != null) { string sql = parents.ToTraceString(); } }
EF4.1及其以上:
LckDbContext ef = new LckDbContext(); var personInfo = from a in ef.Persons.Include("Photo") select a; Console.WriteLine(personInfo.ToString());
注:
- 以上方法在Linq语法时支持友好,但在Lamda函数式查询时不是很好。
- 其他方式,SqlServer中可以用SQL Server Profiler监控等
http://www.cnblogs.com/LingzhiSun/archive/2011/05/05/EF_Trick5.html