views:

3897

answers:

3

Hi, I've been encountering the following error when trying to build a session factory:

PersistenceTests.Can_Map_Orders_To_Database : Failed 
System.IndexOutOfRangeException: Index was outside the bounds of the 
array. 
at NHibernate.Mapping.Column.set_Name(String value) 
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindColumns(XmlNode node, 
SimpleValue model, Boolean isNullable, Boolean autoColumn, String 
propertyPath) 
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindColumnsOrFormula 
(XmlNode node, SimpleValue simpleValue, String path, Boolean 
isNullable) 
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindSimpleValue(XmlNode 
node, SimpleValue model, Boolean isNullable, String path) 
at 
NHibernate.Cfg.XmlHbmBinding.CollectionBinder.BindCollectionSecondPass 
(XmlNode node, Collection model, IDictionary`2 persistentClasses) 
at 
NHibernate.Cfg.XmlHbmBinding.CollectionBinder.<>c__DisplayClassd.<AddCollec tionSecondPass>b__c 
(IDictionary`2 persistentClasses) 
at NHibernate.Cfg.Configuration.SecondPassCompile() 
at NHibernate.Cfg.Configuration.BuildSessionFactory() 
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in c: 
\Code Samples\NHibernate\Fluent Nhibernate - Trunk\src\FluentNHibernate 
\Cfg\FluentConfiguration.cs: line 94 
FluentNHibernate.Cfg.FluentConfigurationException: An invalid or 
incomplete configuration was used while creating a SessionFactory. 
Check PotentialReasons collection, and InnerException for more detail. 
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in c: 
\Code Samples\NHibernate\Fluent Nhibernate - Trunk\src\FluentNHibernate 
\Cfg\FluentConfiguration.cs: line 99 
at FluentNHibernate.SessionSource..ctor(FluentConfiguration config) in 
c:\Code Samples\NHibernate\Fluent Nhibernate - Trunk\src 
\FluentNHibernate\SessionSource.cs: line 38 
at 
FluentNHibernate.Testing.SingleConnectionSessionSourceForSQLiteInMemoryTest ing..ctor 
(FluentConfiguration config) in c:\Code Samples\NHibernate\Fluent 
Nhibernate - Trunk\src\FluentNHibernate\Testing 
\SingleConnectionSessionSourceForSQLiteInMemoryTesting.cs: line 15 
at Core.Infrastructure.Data.NHibernate.Tests.PersistenceTests.SetUp() 
in PersistenceTests.cs: line 26

I'm working against the FNH trunk and NH 2.0.1. Funny thing is I am able to compile my mappings (via AutoPersistenceModel.CompileMappings) and write them to the file system successfully - FNH doesn't complain. It is only when attempting to build the session factory that everything goes kaboom with the not very helpful error message above. Anyone got any ideas?

A: 

We'll need to see your Configuration to help much. But this part of the stack trace should give you an idea of where to start.

at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in c: 
\Code Samples\NHibernate\Fluent Nhibernate - Trunk\src\FluentNHibernate 
\Cfg\FluentConfiguration.cs: line 94 
FluentNHibernate.Cfg.FluentConfigurationException: An invalid or 
incomplete configuration was used while creating a SessionFactory. 
Check PotentialReasons collection, and InnerException for more detail.
Steven Lyons
+5  A: 

Fluent NHibernate itself rarely complains at you directly. Internally it's just building up your HBM files for you so if you told it to build something wrong then NHibernate proper will get grumpy.

If you're not already, you might want to start with exporting your mapping files like so:

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyClass>()
    .ExportTo("path")

Then you can dig around in there to see if something is wrong. I do recall getting this error once before and I think it relates to a mismatch in the number of columns mapped. It was one of those easy-to-miss errors in my mapping, so unfortunately all I can suggest is to really scour the output of ExportTo for anything that doesn't make sense.

Stuart Childs
After mucking about through 90+ mapping files, it seems FNH wasn't generating column names for some many-to-many and one-to-many relationships I'd specified in mapping overrides (implementations of IAutoMappingOverride). Usually that kind of stuff is handled by conventions, but they weren't.
Jimit
For anyone else who encounters this problem, FNH seems to map entities using first the automapper, then user-defined conventions, then default conventions and finally your mapping overrides. Thus conventions will not be executed after calling your overrides and thus you must be explicit.
Jimit
+5  A: 

I found that the inner exception gave more details. In my case I had to add the NHibernate.ByteCode.Castle.dll file to the references.

Francois Botha