Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’

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; }
}

I hope this helps someone.

Till next time,


Posted 03-19-2009 11:00 AM by Derik Whittaker

[Advertisement]

Comments

Bill Campbell wrote re: NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’
on 03-23-2009 2:15 PM

Hi Derik,

Thanks for the great info here! This is really current stuff. I'm getting this same Invalid Index error but I am using the same key for part of a composite-id and then again in the References. Do you know of any work arounds for this?

   public class RouteObjMap : ClassMap<RouteObjNH>

   {

       public RouteObjMap()

       {

           WithTable("Route");

           UseCompositeId()

               .WithKeyProperty(x => x.ProjectID)

               .WithKeyProperty(x => x.RouteID);

           Map(x => x.StartTime);

           Map(x => x.SlackTime);

           Map(x => x.VehicleId);

           Map(x => x.MilesTraveled);

           // Reference back to parent Project

           References(x => x.Project).Cascade.All()

               .WithForeignKey("ProjectID").WithColumns("ProjectID");

       }

   }

Gauthier Segay wrote re: NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’
on 03-29-2009 6:49 AM

Hi Derik, I've also encountered this issue (mostly with composite keys) and have used this workarround:

set  Insert/Update attributes to false on one of theses "redundant" properties.

Steve wrote re: NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’
on 04-02-2009 6:06 AM

the great thing i have discovered about

# public Int32 LevelTypeID  

# {  

#     get { return EpisodeLevel.LevelTypeID; }  

# }

is that nhib does not send off another query to get the EpisodeLevel project. It knows it can pull the id out of its on internal FK column. nice.

If (!awesome) return "fail"; wrote Invalid index N for this SqlParameterCollection with Count=N.
on 07-08-2009 6:44 PM

Invalid index N for this SqlParameterCollection with Count=N.

Nathan wrote re: NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’
on 08-03-2009 12:52 AM

Thanks Derik - Been using Castle ActiveRecord for a bit and recently I've been learning Fluent NHibernate.  I was beating my head against the desk for a good while before I found your blog post.  Thanks!

curlyfro wrote re: NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’
on 08-19-2009 12:01 PM

you saved me hours -- thx

Per Erik Stendahl wrote re: NHibernate and ‘Invalid Index N for this SqlParameterCollection with Count=N error’
on 09-10-2009 4:00 AM

Thanks!

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)