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?

1

1 Answer 1

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!

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!
    – PJUK
    Jan 18, 2012 at 14:59
  • Agreed, this helped to know what to look for. Thanks for sharing.
    – Nexxas
    Jul 30, 2012 at 16:14
  • 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!
    – CMPerez
    Oct 18, 2013 at 7:13
  • Thank you for clarifying an unhelpful error message.
    – Duane
    Jan 27, 2015 at 23:07
  • 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); });
    – COBRA.cH
    May 18, 2017 at 9:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.