Troubleshooting
Sometimes your mapping won't be accepted by NHibernate engine. Unfortunatelly it doesn't throw an exception, so you may have a problem later. In situations like that, just enable NHibernate logging to see what's wrong.
Enabling NHibernate logging
NHibernate uses log4net, so it's just a matter of configuring it. For example, to have a log.txt file with all debug information you can get, use:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" /> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <activerecord> ... </activerecord> <log4net> <!-- Define some output appenders --> <appender name="trace" type="log4net.Appender.TraceAppender, log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%P{user}&gt; - %m%n" /> </layout> </appender> <appender name="console" type="log4net.Appender.ConsoleAppender, log4net"> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%P{user}&gt; - %m%n" /> </layout> </appender> <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="log.txt" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy.MM.dd" /> <param name="StaticLogFileName" value="true" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n" /> </layout> </appender> <root> <!-- priority value can be set to ALL|INFO|WARN|ERROR --> <priority value="ALL" /> <appender-ref ref="rollingFile" /> </root> </log4net> <nhibernate> <!-- with this set to true, SQL statements will output to the console window if it's a console app --> <add key="hibernate.show_sql" value="true" /> </nhibernate> </configuration>
After that, make sure you're initializing the log4net config in your code by putting the line
log4net.Config.XmlConfigurator.Configure();
before your
ActiveRecordStarter.Initialize(...)
call.
Then, run your test cases and check the log file for Exceptions - yes, do a search. There will be plenty of information about what went wrong. Note that log.txt will be in the same directory as your EXE or Web application directory – not necessarily where your .csproj file is.
Changes to objects are persisted without an explicit call to the Save() method
This commonly happens if your the SessionScope per request pattern (described here Enable Session per Request).
Basically - this is expected NHibernate behaviour - although the first time you experience it - it can seem very confusing.
From: NHibernate Users FAQ![]()
"When an object is loaded by NHibernate the ISession keeps a snapshot of the state of your object. When you Flush() the ISession NHibernate compares that snapshot to the current state of the object. The appropriate changes are written to the database."
So - when the Session is Flush()ed any changes to an objects state are written to the database. The next question is when does a Flush() occur? Well according to Tobins' NHibernate FAQ
- it can occur at a number of different (and possibly unexpected) places.
There is a short term work around for this problem. (hammett describes it as palliative). When you dispose your SessionScope - use this override for SessionScope.Dispose(true) - which says not to persist any unsaved changes in this session.
Text Columns are truncated in Sql Server
This is a nhibernate bug which should be fixed soon. The solution is to add the ColumnType="StringClob" parameter to your Property attribute. e.g.
[Property(ColumnType="StringClob")] public String Contents { get { return _contents; } set { _contents = value; } }
This tells nhibernate to map the string to a Text type rather than a nvarchar(4000) type.
Common Errors
ActiveRecord throws an exception saying: Ambiguous column name 'Status'.
A column named Status is returned internally by NHibernate. Try renaming your column in the database.
