SQL 如何知道 Fluent NHibernate 生成的查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/819040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know query generated by Fluent NHibernate
提问by Rahul Somwanshi
I am using linq to Nhibernate to fire some select query to data base.
我正在使用 linq 到 Nhibernate 来向数据库发出一些选择查询。
My question is, how do I know, the query generated by Fluent NHibernate?
我的问题是,我怎么知道 Fluent NHibernate 生成的查询?
采纳答案by Joey V.
If you want the SQL to be in log4net, make sure you set the logger in your configuration section.
如果您希望 SQL 在 log4net 中,请确保在配置部分中设置记录器。
I put the NHibernate package at "INFO" to reduce the noise and the NHibernate.SQL to all so I can log all SQL statements.
我将 NHibernate 包放在“INFO”以减少噪音,并将 NHibernate.SQL 放在所有位置,以便我可以记录所有 SQL 语句。
<logger name="NHibernate"> <level value="INFO" /> </logger> <logger name="NHibernate.SQL"> <level value="ALL" /> </logger>
回答by Kevin Berridge
With Fluent NHibernate, you can turn on show_sql
like this:
使用 Fluent NHibernate,您可以show_sql
像这样打开:
Fluently.Configure()
.Database( MsSqlConfiguration.MsSql2005.ShowSql().ConnectionString(...) )...
NHibernate will now print every sql statement to Console.Out
.
NHibernate 现在会将每个 sql 语句打印到Console.Out
.
回答by Chris Canal
You might also find this useful http://nhprof.com/
您可能还会发现这很有用http://nhprof.com/
回答by Jugal Panchal
I have found 4 options to know sql query in nhibernate and fluent nhibernate.
我找到了 4 个选项来了解 nhibernate 和 fluent nhibernate 中的 sql 查询。
- Log - Joey V. said in answer of this same question.
- ShowSql - Kevin Berridge said in answer of this same question.
- NHProf - This is a awesome profiler. NHProf
Intercepter - It is really good to see sql. we can put it in our Output of Visual Studio and even in log file.
ISessionFactory sf = Fluently.Configure() .Database(MySQLConfiguration.Standard.ConnectionString(ConnectionString).ShowSql()) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Stock>()) .ExposeConfiguration(c => c.SetInterceptor(new ABCInterceptor())) .BuildSessionFactory(); public class ABCInterceptor : EmptyInterceptor { public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql) { Trace.WriteLine(sql.ToString()); return sql; } }
- Log - Joey V. 在回答同一个问题时说。
- ShowSql - 凯文·贝里奇 (Kevin Berridge) 在回答同一个问题时说。
- NHProf - 这是一个很棒的分析器。NHProf
Intercepter - 看sql真是太好了。我们可以把它放在我们的 Visual Studio 输出中,甚至放在日志文件中。
ISessionFactory sf = Fluently.Configure() .Database(MySQLConfiguration.Standard.ConnectionString(ConnectionString).ShowSql()) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Stock>()) .ExposeConfiguration(c => c.SetInterceptor(new ABCInterceptor())) .BuildSessionFactory(); public class ABCInterceptor : EmptyInterceptor { public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql) { Trace.WriteLine(sql.ToString()); return sql; } }
回答by Deeksy
回答by Paco
You can also hook in log4net.
您也可以挂入 log4net。