Skip to content

Allowing NHibernate to have multiple foreign keys without the Invalid Index

July 13, 2012

Arghh, I thought I had already wrote this post yesterday, obviously WordPress hasn’t registered it, and why does the PC have to run agonisingly slow for me to write the post before I leave work! Grrr!! Anyway before I throw my frustrations onto the LED monitor, I would like to write again why NHibernate which after tweaking a lot causes the following error: –

Invalid index n for this SqlParameterCollection with Count=n.

Ok being really patient with the writing here, PC running extremely slow when typing the words don’t appear till 2 minutes later!

If you receive the error on invalid index n, you may be wondering why it shows up as an error in NHibernate when you know the mappings on the xml file are correct, you need foreign keys to map on your table to link relationships to other tables, and you also use the many-to-one tag inside. So you use two tags pointing to the same id, which may cause the error however you need them both because you want to create a static method which does a search on the foreign keys using NHibernate. So you are stuck! THis is what I felt after a couple of hours tweaking both tags, well here is the solution: –

<property name="AssociationId" column="AssociationColumnName" update="false" insert="false" />
<many-to-one name="Association" column="AssociationColumnName" property-ref="Id" not-null="false" />

You add an update="false"and insert="false" to the single property tag id.
You then add a not-null="false" to the many-to-one tag.


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: