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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:56:19  来源:igfitidea点击:

How to know query generated by Fluent NHibernate

sqlnhibernateloggingoutput

提问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_sqllike 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 查询。

  1. Log - Joey V. said in answer of this same question.
  2. ShowSql - Kevin Berridge said in answer of this same question.
  3. NHProf - This is a awesome profiler. NHProf
  4. 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;
        }
     }
    
  1. Log - Joey V. 在回答同一个问题时说。
  2. ShowSql - 凯文·贝里奇 (Kevin Berridge) 在回答同一个问题时说。
  3. NHProf - 这是一个很棒的分析器。NHProf
  4. 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 Anton Gogolev

See this. What you need is hibernate.show_sql.

看到这个。你需要的是hibernate.show_sql

回答by Deeksy

Definitely buy and use NHProf. This is an awesome product and not only shows you what queries are being run, but also shows you any potential performance problems with your NHibernate mappings and queries.

绝对购买和使用NHProf。这是一个很棒的产品,不仅可以向您展示正在运行的查询,还可以向您展示 NHibernate 映射和查询的任何潜在性能问题。

回答by Arnis Lapsa

You can use sql profilers like this onetoo.

您也可以使用像这样的sql 分析器。

回答by Paco

You can also hook in log4net.

您也可以挂入 log4net。