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?
This is not possible unfortunately.
If you want hard to do this take a look in the code below.
More code to guide you to a working solution
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.