5

I have a rather complex entity which will not save when a particular database table is missing a record. When the record exists the entity saves correctly. When the record does not I receive the exception:

Invalid index N for this SqlParameterCollection with Count=N

After reading a bunch of solutions found via Google and the most closely related questions on Stack Overflow:

I believe my issue has to do with the way I have my mapping files setup. The Customer entity has reference to the Person entity. Person maps to a table which we have read, but not write access to. It is when a record for the Person entity does not exist that I generate the exception. If the record exists no issue. I've set the reference to Person from customer to Nullable(). I have also double checked to ensure I do not have a property mapped twice from either entity.

Here is what I feel is the pertinent mapping information, but can provide more as needed:

Customer

//more mapping code...
References(x => x.Person, "snl_id").Nullable();
//more mapping code...

Person

//more mapping code...
ReadOnly();
Id(x => x.SnlId).Column("SNL_ID");
//more mapping code...

To further complicate matters we have some painful code to make NHibernate perform better when Person does not exist. I am not sure it applies here, but thought it pertinent enough to include in my question. We are using the below code because without it the NHibernate JIRA will create tons of queries. This solution is outlined in this Stack Overflow answer.

Customer's person property

public virtual Person Person
{
    get
    {
        try
        {
            var snlId = per.Name;
            return per;
        }
        catch
        {
            return null;
        }
    }
    set
    {
        per = value;
    }
}

private EPerson per;

What am I missing in my mappings that would cause this exception? Is there another piece of this problem that I am not seeing?

Community
  • 1
  • 1
ahsteele
  • 26,243
  • 28
  • 134
  • 248

2 Answers2

2

While Scott's solution of removing the snl_id property from the Customer class fixes the issue it causes problems that I cannot get around-- the snl_id can exist in the Customer table even there is not a corresponding Person table record. Since that is the case there are times when I will need access to the snl_id when I cannot get to it via the associated Person property.

I considered several alternative solutions but settled on creating a view of the Customer table including the Customer table primary key and the snl_id from the customer table. Then mapping that property via a join to the view.

Join("v_cust_id_snl_id", j => j.KeyColumn("cust_id").Map(x => x.SnlId, "snl_id")

This change allowed me to have my cake and eat it to. I was able to keep the SnlId property on customer, but no longer throw the exception when saving.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
1

Do you have the snl_id referenced as a property in Customer as well as being the primary key for the child object? If so, this is causing the error you are receiving. Remove the property from Customer and use Person to get the value.

sgriffinusa
  • 4,203
  • 1
  • 25
  • 26