如何获取由Linq2SQL ORM生成的sql字符串? [英] How can I get sql string, generated by Linq2SQL ORM?
问题描述
因此,实际上这是一个主要问题:如何获取由Linq2SQL ORM生成的sql字符串? 我熟悉其他ORM系统,例如nHibernate和EntityFramework(4+). 例如,对于nHibernate,存在 profiler 允许捕获任何由nHibernate引擎执行的活动.另一方面,EntityFramework具有内置功能-方法
So, actually this is a main question : How can I get sql string, generated by Linq2SQL ORM? I am familiar with other ORM systems, such as nHibernate and EntityFramework (4+). For instance, for nHibernate exist profiler witch allow to catch any activity, performing by nHibernate engine. In the other hand, EntityFramework has build-in functionality - method
query.ToSqlString()
那Linq2SQL呢?
And what about Linq2SQL?
推荐答案
如@usr所述,您可以在查询中使用ToString()
来获取生成的SQL.您还可以使用 Log
属性在linq数据上下文上转储所有SQL命令.
As @usr mentions, you can use ToString()
on the query to get the generated SQL. You can also use the Log
property on the linq data context to dump all SQL commands.
另一个通用选项是使用SQL Server Profiler监视所有查询进入服务器时的情况.这将适用于所有ORM.
Another general option is to use the SQL Server Profiler to watch all queries as they come in to the server. That will work for all ORM.
这篇关于如何获取由Linq2SQL ORM生成的sql字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!