7

I have an MVC app using NHibernate.

I created a model and tried to insert data into the db for the first time with NHibernate, so please go easy.

I get this error on the session.Save(object);

IndexOutOfRangeException was unhandled by user code

Invalid index 8 for this SqlParameterCollection with Count=8.

When I look at the data in the object using watch window all seems ok and also collections are inittialized.

Using SQL 2005.

Any ideas why this may be occurring please?

Malcolm

3 Answers 3

12

I have had the same problem using NHibernate myself, the problem in my case was in the mapping. On my associations I have mapped a property containing the associated entity, and also a property with the association ID only.

The solution in my case was to add update="false" insert="false" to my mapping of the property containing the ID. Like this:

<property name="AssociationId" column="AssociationColumnName" update="false" insert="false" />
<many-to-one name="Association" column="AssociationColumnName" property-ref="Id" not-null="false" />
Sign up to request clarification or add additional context in comments.

2 Comments

I've ran into that error as well a few times, since it occurs very rarely, I have specifically caught the exception and wrote a reminder in the log for myself
wow! stackoverflow + fredrik ftw! thanks for saving me a ton of time on this issue.
2

Guys if you are getting this Check the mapping file, i got the same Error and i had two propertise mapped to the same column, i know the answer is pretty late this is for the users who come in looking for answers for the same question.

Note: this might not be the only reason, anyways mine was due to this.

Comments

0

Does one of these links help?

http://groups.google.com/group/nhusers/browse_thread/thread/34f124a7052c37ff

http://www.methodicmadness.com/2009/01/nhibernate-what-is-heck-clazz.html

http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/116c822c44aae797

If not, you'll have to post your mapping to get additional advice.

I suggest troubleshooting this by first isolating the insert into a unit test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.