ef多条件查询 如何得到EF查询生成的SQL?

如何得到EF查询生成的SQL?在ef4.1中,我们可以直接调用dbquery<>的toString()方法来获取生成的SQL。在中使用(var context=new MyDbContext(

如何得到EF查询生成的SQL?

在ef4.1中,我们可以直接调用dbquery<>的toString()方法来获取生成的SQL。在中使用(var context=new MyDbContext()){var people=from p背景。人其中p.PersonID> 100选择p string sql=人物.ToString()}生成的SQL是:选择[extend1]。[personid]为[personid],[extend1]。[name]作为[dbo]的[name]。[people]as[extend1]where[extend1]。[personid]>您应该猜到这里的toString()方法实际上调用了objectquery的totracestring()方法<>。数据库查询<>.TO管柱()==& gtSystem.Data.Entity实体. 内部.Linq.InternalQuery<>. tostring()方法,在中实现。Net反射程序如下所示:public override string tostring(){返回这个uobjectQuery.ToTraceString()}