Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get NHibernate to give me the SQL it would generate for an insert / update instead of executing it?

I'm using NHibernate to map some very simple entities to three tables.

My program needs to have a mode where it outputs the SQL it would execute to insert or update these tables, so that the SQL can be saved to a script file, to be later executed manually.

This is similar to this question (which has a very nice answer): How can I have NHibernate only generate the SQL without executing it?

Only that question is about SELECT, I need kind of the same but for INSERT / UPDATE. This also needs to be dynamic because it depends on a user option. I mean, when my program does:

        session.Save(entity);

sometimes I need that to hit the db and sometimes I need it to output the SQL it would execute.

Is this at all possible?

like image 294
Alvaro Rodriguez Avatar asked Dec 04 '25 16:12

Alvaro Rodriguez


1 Answers

It's not possible as that's the main purpose of having ORM like NHibernate but for testing purposes if you want to check the queried generated by Nhibernate then use this logger in app.config ,

<logger name="NHibernate.SQL">
  <level value="ALL" />
  <appender-ref ref="NHibernateRollingLogFileAppender" />
</logger>

In then, when you stary your project it should generate logfile_nhibernate.txt and you will see all the generated queries.

like image 108
S52 Avatar answered Dec 06 '25 06:12

S52