1

NHibernate ERROR Message Invalid index 4 for this SqlParameterCollection with Count=4.

My Mapping:

public KazangAccountClassMap()
{
    Table("KazangAccount");
    Id(x => x.Id).GeneratedBy.Identity();
    Map(x => x.Channel).Not.Nullable();
    Map(x => x.UserName).Not.Nullable();
    Map(x => x.Password).Not.Nullable();
    HasMany<KazangMerchant>(x => x.KazangMerchants)
      .KeyColumn("AccountId")
      .Inverse();
}

CONTROLLER:

public ActionResult SaveChannelProperty(string NewProp, string ChannelName)
{
    client.CreateKazangChannelAttribute(new KazangChannelAttr
    {
        KazangChannelId = ChannelObject.Id,
        AttrName = NewProp,
        AttrValue = NewProp,
        AttrType = ""          
    });
}
1
  • This error is simple to find and also simple to fix, but please, show the full exception stack trace and the mapping related to that error. Commented Jan 12, 2015 at 13:18

1 Answer 1

5

This kind of exception is almost always related to "doubled" mapping. Because from the snippets shown in the quesiton it is not easy to show, I would explain it like this:

Let's have an entity KazangAccount, which has reference to Channel and also int representation of that object as ChannelId

public class KazangAccount
{
    public virtual int Id { get; set; }
    ...
    public virtual Channel Channel { get; set; }
    public virtual int ChannelId { get; set; }
}

In this case, we would be intstructing NHibernate to use ONE column for TWO properties - and that would not work:

public KazangAccountClassMap()
{
    ...
    References(x => x.Channel)
       .Column("Channel_ID"); // the Channel reference
    Map(x => x.ChannelId)
       .Column("Channel_ID");      // the int property ChannleId

That mapping is good for any READ operation... column will be used twice. But for WRITE operation, we would create params for column Channel_ID and another for column Channel_ID == only one of them will be created. And while NHibernate expects some count of params - there is one missing.

Solution? Make one of these read-only

public KazangAccountClassMap()
{
    ...
    References(x => x.Channel)
       .Column("Channel_ID"); // READ and WRITE
    Map(x => x.ChannelId)
       .Column("Channel_ID")  // just READ
       .ReadOnly()            // insert="false" update="false"
       ; 

Now, NHibernate will create/expect only one column/sqlParamter for this column...

Sign up to request clarification or add additional context in comments.

2 Comments

As said in answer problem is related to double mapping. In my case it was in SubclassMap. Each "SubClass" was mapping the same base property.
Good if you've found the way... ;)

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.