What are the differences between HasOne and References in nhibernate?

HasOne creates a one-to-one mapping between tables for you. References creates a typical relational many-to-one relationship. More defined: a one-to-one relationship means that when one record exists in one table, it must (or can) have one and at most one record in the other referenced table. Example: User table and Options table (one user has … Read more

Fluent NHibernate – Create database schema only if not existing

You can just use SchemaUpdate instead, it will update the schema if it exists and create it if it does not: public NhibernateSessionFactory(IPersistenceConfigurer config) { _sessionFactory = Fluently.Configure(). Database(config). Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>()). ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)). BuildSessionFactory(); } One caveat: SchemaUpdate does not do destructive updates (dropping tables, columns, etc.). It will only add … Read more

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

How can I recreate this complex SQL Query using NHibernate QueryOver?

I have manage to achieve such dynamic search criterion by using Criteria API’s. Problem I ran into was duplicates with inner and outer joins and especially related to sorting and pagination, and I had to resort to using 2 queries, 1st query for restriction and using the result of 1st query as ‘in’ clause in … Read more

Override for fluent NHibernate for long text strings nvarchar(MAX) not nvarchar(255)

Adding this convention will set the default length for string properties to 10000. As others have noted, this will be a nvarchar(max) column. public class StringColumnLengthConvention : IPropertyConvention, IPropertyConventionAcceptance { public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) { criteria.Expect(x => x.Type == typeof(string)).Expect(x => x.Length == 0); } public void Apply(IPropertyInstance instance) { instance.Length(10000); } } Conventions can … 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