EF-查看生成的SQL语句

老版本中: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();
    }
}
View Code

EF4.1及其以上:

            LckDbContext ef = new LckDbContext();
            var personInfo = from a in ef.Persons.Include("Photo")
                             select a;         
            Console.WriteLine(personInfo.ToString());
View Code

 

注:

  • 以上方法在Linq语法时支持友好,但在Lamda函数式查询时不是很好。
  • 其他方式,SqlServer中可以用SQL Server Profiler监控等

 

http://www.cnblogs.com/LingzhiSun/archive/2011/05/05/EF_Trick5.html

posted on 2014-01-10 11:52  顺风车  阅读(2386)  评论(0编辑  收藏  举报