23

I am using linq to Nhibernate to fire some select query to data base.

My question is, how do I know, the query generated by Fluent NHibernate?

HerbalMart
  • 1,669
  • 3
  • 27
  • 50
Rahul Somwanshi
  • 455
  • 2
  • 5
  • 19

8 Answers8

37

With Fluent NHibernate, you can turn on show_sql like this:

Fluently.Configure()
    .Database( MsSqlConfiguration.MsSql2005.ShowSql().ConnectionString(...) )...

NHibernate will now print every sql statement to Console.Out.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Kevin Berridge
  • 6,251
  • 6
  • 41
  • 42
  • 1
    Hi Kevin, Thanks for posting this! I'm wondering if you've also hooked up Log4Net so it sends the generated SQL to a file? – 5x1llz Jun 06 '09 at 02:04
  • 14
    This is so frustrating. Any idea why this would ever not work? I'm looking through my Console and it's just not there. – Milimetric Jun 14 '11 at 22:26
6

If you want the SQL to be in log4net, make sure you set the logger in your configuration section.

I put the NHibernate package at "INFO" to reduce the noise and the NHibernate.SQL to all so I can log all SQL statements.

  
 <logger name="NHibernate">
   <level value="INFO" />
 </logger>


  <logger name="NHibernate.SQL">
    <level value="ALL" />
  </logger>

Joey V.
  • 1,866
  • 1
  • 22
  • 18
6

I have found 4 options to know sql query in nhibernate and fluent nhibernate.

  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;
        }
     }
    
Jugal Panchal
  • 1,448
  • 16
  • 20
6

You might also find this useful http://nhprof.com/

Chris Canal
  • 4,824
  • 9
  • 35
  • 45
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Mat Nov 07 '18 at 08:33
2

See this. What you need is hibernate.show_sql.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
0

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.

Deeksy
  • 5,304
  • 2
  • 23
  • 27
0

You can use sql profilers like this one too.

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
-1

You can also hook in log4net.

Paco
  • 8,335
  • 3
  • 30
  • 41