Monday, 22 June 2009

NHibernate Parameter Error

We were getting an IndexOutOfRange Exception when trying to save a Period object in our application. Invalid Index 17 for this SqlParameterCollection with Count=17

It was a little confusing as we'd not seen any error like this before:

After a bit of head scratching, we interrogated the mapping file, see if you can spot the error:

Did you spot it? Well, there are two columns called "MORId", one in the main class, and one in the component AgreedWorkLevel. The important thing to remember is that components are stored in the same table and although they are in different classes, they need to be uniquely named.

I would imagine if you wanted the component property to be the same as the class' property, you could link this in code and leave NHibernate pointing to the single reference point.


  1. I expect that is because normally NH uses ComponentName_PropertyName for the column doesn't it? You are explicitly setting it.

  2. @Peter, yes you are correct. We wanted to keep the column name short as it had to work with Oracle and there seems to be limits there.

    The real answer, is probably that the MOR is not needed on the AgreedWorkLevel since it is on the Period class anyways. Why have it twice?

    I love converting existing applications :)