Upgrading to NHibernate 2.0 alpha 1
Posted by Davy Brion on April 6th, 2008
I just wasted time on something really stupid while i was upgrading to NHibernate 2.0 alpha 1, so i’m posting it here just in case someone else runs into this…
With NHibernate 1.2, i created my SessionFactory like this:
Configuration configuration = new Configuration()
.AddAssembly(“MyMappingAssembly”);
_sessionFactory = configuration.BuildSessionFactory();
But it didn’t work with NHibernate 2.0, i got the following exception:
failed: NHibernate.MappingException : Could not compile the mapping document: Northwind.Domain.Mappings.Region.hbm.xml ----> System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary. TearDown : System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ----> System.NullReferenceException : Object reference not set to an instance of an object. c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(262,0): at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(436,0): at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(1557,0): at NHibernate.Cfg.Configuration.ProcessMappingsQueue() c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(1548,0): at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(1541,0): at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(506,0): at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(544,0): at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(615,0): at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(601,0): at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName) C:\mydocs\src\Northwind\trunk\Northwind\Northwind.Tests\Domain\Mappings\NHibernateTest.cs(27,0): at Northwind.Tests.Domain.Mappings.NHibernateTest.SetUp() --KeyNotFoundException at System.ThrowHelper.ThrowKeyNotFoundException() at System.Collections.Generic.Dictionary`2.get_Item(TKey key) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Dialect\Dialect.cs(173,0): at NHibernate.Dialect.Dialect.GetDialect(IDictionary`2 props) c:\DATA\Projects\nhibernate\2.0.x\copy1\nhibernate\src\NHibernate\Cfg\Configuration.cs(428,0): at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
Apparently, it failed when trying to instantiate the Dialect class to use. So i figured something must have changed in the configuration so i checked everything and it looked alright to me. I tried a few things but i just couldn’t get it working, so i downloaded the alpha 1 code so i could step through it and see why it wouldn’t configure properly… turns out that it just didn’t read the configuration settings at all. In NHibernate 1.2, it automatically did this, but now it’s a required explicit step. Changing my above code to this worked:
Configuration configuration = new Configuration()
.Configure()
.AddAssembly(“MyMappingAssembly”);
_sessionFactory = configuration.BuildSessionFactory();

April 8th, 2008 at 3:12 pm
THANK YOU, thought I was going mad…
April 9th, 2008 at 3:18 pm
Thanks heaps for this!
April 22nd, 2008 at 3:35 pm
Cheers Davy, this sure speeded moving my app from NHibernate v1.2 to v2.0.
April 25th, 2008 at 4:43 pm
Awesome! thanks!
April 30th, 2008 at 3:13 pm
You just saved hours of my productive time. And also helped me tremendously selling NHibernate to my colleagues. Can I buy you beer or something? ;=)
April 30th, 2008 at 6:19 pm
umm, sure
May 1st, 2008 at 3:29 pm
Maybe, I’m missing something simple. For NH 1.2 I never used a hibernate.cfg.xml I just put conf in the app.config. For 2.0, once I added the .Configure() to get past the “given key” error you describe, now it is failing because it can’t find hibernate.cfg.xml. Any pointers?
May 1st, 2008 at 3:53 pm
do you have a hibernate-configuration section in your app.config? if not, nhibernate tries to use the hibernate.cfg.xml file. But if the section is there, it should use it.
May 1st, 2008 at 5:26 pm
I did have the nhibernate-configuration section in my app.config working with NH 1.2. Maybe it has to be changed to work with 2.0?
Maybe because it’s alpha, not sure, i wasn’t able to use NH 2.0 alpha with appconfig. using this post, http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx i was able to craft a proper hibernate.cfg.xml and get NH 2.0 alhpa working.
May 14th, 2008 at 4:54 pm
Finally found out how to load from app.config, this is the solution:
In <configSections>:
<section name=”hibernate-configuration” type=”NHibernate.Cfg.ConfigurationSectionHandler, NHibernate”/>
This causes the configuration to be loaded through NHibernate’s ConfigurationSectionHandler class.
June 6th, 2008 at 6:03 pm
You’re the man – it took me three days and a lot of pain until I came across your suggestion. Now I can enjoy the weekend. Thanks a million.
June 10th, 2008 at 9:56 pm
New user here trying to get NHibernate working for the first time. Would never have figured this out for myself; the online docs on the hibernate site really need to change (everyone following the tutorials up there now will have the same issue).