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