If you have ever used NHibernate I am sure you have encounted the error ‘Invalid Index ‘N’ for this SqlParameterCollection with Count=N’ (where N is any number) exception when trying to create your mappings.
If you are completely new to NHibernate, as I am I am sure this one has you scratching your head saying…. WTF.
The good news is that ‘normally’ the solution to this problem is pretty easy, but before I tell you the answer let me explain the issue.
Take a look at the code below
- public EpisodeMap()
- {
- WithTable( "Episode" );
-
- Id( x => x.ID ).GeneratedBy.Identity();
-
- Map( x => x.LevelTypeID );
- Map( x => x.Name );
- Map( x => x.Description );
- Map( x => x.EpisodeNumber );
- Map( x => x.EpisodeDate );
- Map( x => x.CreatedDate );
- Map( x => x.Enabled );
-
- References( x => x.EpisodeLevel )
- .WithForeignKey( "LevelTypeID" ).TheColumnNameIs( "LevelTypeID" )
- .Access.AsCamelCaseField( Prefix.Underscore )
- .FetchType.Join();
-
- }
Take notice to the mapping above. I have a many-to-one mapping for EpisodeLevel, but I have also created and mapped the FK to EpisodeLevel as LevelTypeID.
The issue (as i have experienced it) is this:
Because I have mapped my FK to the Episode Level table as .LevelTypeID as well as to the EpisodeLevel entity NH is going to try to create multiple associations on that field. However it cannot because that is not correct.
To solve this issue (mostly in my experience) all you need to do is remove the following line
Map( x => x.LevelTypeID );
If you MUST populate the LevelTypeID property at the top level, do so by providing the value as a pass through from Episode Level as such:
- public Int32 LevelTypeID
- {
- get { return EpisodeLevel.LevelTypeID; }
- }
NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’ is error related to NHibernate mapping so please check your mapping class mostly with composite keys and foreign keys.
I hope this helps someone.
Thanks,
Rajesh