"Invalid Index n for this SqlParameterCollection with Count=n" OR "foreign key cannot be null"
Asked Answered
A

1

28

I have been successfully using NHibernate for quite some time now and have been able to solve a lot of pitfalls with an application that I developed with it and that is running in production. The recent hurdle really has me scratching my head, though.

Recently I had to expand the class library with some new classes that are nested as children to some already existing classes. I just copied the same model for aggregate mapping that I already was successfully using, but this time it does not work.

Now when I use the following in the parent mapping file:

  <bag name="SeaInfoItems" table="EDIImport_SeaInfo" lazy="false" cascade="save-update"> 
     <key column="EDI_FK_OWNERID"/> 
     <one-to-many class="FargoGate.AppLib.EdiImportSeaInfo, FargoGate.AppLib"/> 
  </bag> 

I can choose to, in the child class, either use:

  <property name="EDI_FK_OWNERID" column="EDI_FK_OWNERID" /> 

...which gives me the infamous "Invalid Index n for this SqlParameterCollection with Count=n" error.

OR I try with this solution I found after some Googling:

   <property name="EDI_FK_OWNERID" column="EDI_FK_OWNERID" insert="false" update="false" /> 

...which gives me a "Cannot insert the value NULL into column 'EDI_FK_OWNERID'... column does not allow nulls." error.

So basically I have to choose between pest and cholera.

What I don't get is that it works flawlessly for the already existing aggregate classes, and I really cannot spot the difference. The only thing is that this foreign key (EDI_FK_OWNERID) could refer to two different parent tables. Bad database design, I know, but I didn't design it, and it is my task to develop up to it for better or worse. I cannot change the database design.

The other difference is that I totally removed the foreign key reference from the already existing child classes (the mappings as well as the class members). I tried to emulate that of course, but of no avail.

Also I discovered that one of the new classes (which is quite small) also works fine. But I cannot see what the difference is here either. I am stumped!

Anyone has a clue?

Ascending answered 3/2, 2011 at 15:30 Comment(1)
actually the invalid index n ... exception whould have led you straight to #2298526Misrule
A
61

Aaargh! I was put so much on a wrong leg with this infamous “Invalid Index n for this SqlParameterCollection with Count=n” error that I overlooked the obvious: A duplicate mapping of a field for ONE of the classes. In that particular mapping I left this error, where the primary key is also defined as a property:

 <id name="ID" column="ID">
   <generator class="guid" />
 </id>
 <property name="ID" column="ID" /> 

Now that was a waste of time trying to debug that!

Ascending answered 4/2, 2011 at 10:26 Comment(5)
+1 for pointing out that it could be a duplicate in ONE of the classes... I had already resolved this for the parent class... forgot completely about a child class!Zinciferous
Agreed, this helped to know what to look for. Thanks for sharing.Sagittal
A lot later, but thanks. I'm working with legacy databases too, and now I'm working with a table that has 45 columns, and I had no idea what was wrong... and bingo, two properties were mapped to the same column because of a copypasta typo. Thanks!Danie
Thank you for clarifying an unhelpful error message.Petry
Thanks mate. That saved me a lot of time. In my case, it was the following duplicate mapping: Property(x => x.PositionID, map => map.NotNullable(true)); ManyToOne(x => x.OffertePosition, map => { map.Column("PositionID"); map.Cascade(Cascade.None); });Dayle

© 2022 - 2024 — McMap. All rights reserved.