In the previous article we mentioned that you can monitor the SQL scripts nhibernate send to the database by overriding the NHibernate Emptyinterceptor interceptor. See a friend today with the LINQPad tool for nhibernate generated SQL statement monitoring, try to feel more intuitive. Of course this is not the main function of LINQPad, but we can use this method to achieve our goal.
Next, let's look at how to use LINQPad to view the SQL NHibernate sent to the database.
1. Press F4 to enter Query Properties, or Query->query propertiesl. Add NHibernate and the entity-related DLL file you are using here.
2. In the Additional Namespace Imports tab, click Pick from assemblies to add the namespaces used.
3. Go back to the main interface, write the test code, and execute it.
This gives you a visual view of the resulting SQL statement and the results of the query.
Configuration config =NewConfiguration (). Configure (@"D:\Norton.Demo\NHibernate\Norton.Demo.NHibernate\NHibernate\hibernate.cfg.xml");varSessionfactory =CONFIG. Buildsessionfactory ();using(varSession =sessionfactory.opensession ()) { varList = session. Createcriteria<product>() . ADD (Restrictions.eq ("Name","Apple")) . Uniqueresult<Product>(); List. Dump ();}
View Code
[NHIbernate] using LINQPad to view NHIbernate generate SQL statements