How to log SQL calls with NHibernate to the console of Visual Studio?

To show the SQL in the output window of Visual Studio, configure log4net to use TraceAppender in your log4net config. This: <appender name=”DebugSQL” type=”log4net.Appender.TraceAppender”> <layout type=”log4net.Layout.PatternLayout”> <conversionPattern value=”%date [%thread] %-5level %logger [%property{NDC}] – %message%newline” /> </layout> </appender> Then this: <logger name=”NHibernate.SQL” additivity=”false”> <level value=”DEBUG” /> <appender-ref ref=”DebugSQL” /> </logger> EDIT: I can’t seem to format … Read more

The length of the string value exceeds the length configured in the mapping/parameter

This is a well known issue with NHibernate handling nvarchar(max), see : http://geekswithblogs.net/lszk/archive/2011/07/11/nhibernatemapping-a-string-field-as-nvarcharmax-in-sql-server-using.aspx For some years now, I have been file mapping nvarchar(max) columns to StringClob without encountering any problem : <property name=”myProp” column=”MY_PROP” not-null=”true” type=”StringClob” access=”property”></property> This link (from the comments) describes the fluent mapping required to fix this issue: https://www.tritac.com/nl/blog/fluent-nhibernate-nvarchar-max-fields-truncated-to-4000-characters/ Map(x => x.Description).CustomType(“StringClob”).CustomSqlType(“nvarchar(max)”);

What should be the lifetime of an NHibernate session?

You want a session management strategy that allows your app to function effectively and take advantage of the things that NHibernate gives you – most notably caching and lazy loading. Creating sessions is an inexpensive process and requires little up-front RAM or CPU, so you shouldn’t worry about conserving or re-using sessions (indeed, re-using them … Read more

How do I view the SQL that is generated by nHibernate?

You can put something like this in your app.config/web.config file : in the configSections node : <section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler,log4net”/> in the configuration node : <log4net> <appender name=”NHibernateFileLog” type=”log4net.Appender.FileAppender”> <file value=”logs/nhibernate.txt” /> <appendToFile value=”false” /> <layout type=”log4net.Layout.PatternLayout”> <conversionPattern value=”%d{HH:mm:ss.fff} [%t] %-5p %c – %m%n” /> </layout> </appender> <logger name=”NHibernate.SQL” additivity=”false”> <level value=”DEBUG”/> <appender-ref ref=”NHibernateFileLog”/> </logger> </log4net> … Read more

Fluent NHibernate: How to create one-to-many bidirectional mapping?

To get a bidirectional association with a not-null foreign key column in the Details table you can add the suggested Owner property, a References(…).CanNotBeNull() mapping in the DetailsMap class, and make the Summary end inverse. To avoid having two different foreign key columns for the two association directions, you can either specify the column names … Read more

Tradeoffs using NHibernate 3.0 QueryOver or LINQ provider

LINQ and QueryOver are completely different query methods, which are added to the ones that existed in NHibernate 2 (Criteria, HQL, SQL) QueryOver is meant as a strongly-typed version of Criteria, and supports mostly the same constructs, which are NHibernate-specific. LINQ is a “standard” query method, which means the client code can work on IQueryable … Read more

Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct

While similar names, the usage is different. I. Projections.distinct(Projections.property(“id”)); this statement would be translated into SQL Statement. It will be passed to DB Engine and executed as a SQL DISTINCT. See: 17.9. Projections, aggregation and grouping so e.g. this example: List results = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.distinct(Projections.property(“id”)) ) ) .list(); would seems like: SELECT … Read more