2

I'm getting this nasty error in Castle Active Record (wrapped around NHibernate) when I try to save a class:

Invalid index n for this SqlParameterCollection with Count=m

I know that this error is caused by a property being mapped multiple times in a class however I'm not sure how to get around it. I have two child classes that both map back to the class in question using the same column (IpAddressNumber). Also IpAddressNumber is the primary key of the class, which results in NHibernate trying to map the IpAddressNumber property three times (just a guess.)

Here is the class:

[ActiveRecord(Lazy=true)]
    public class DeviceConfiguration : UsersDatabase<DeviceConfiguration>
    {
        [PrimaryKey]
        public virtual long IPAddressNumber { get; set; }

        [BelongsTo("IPAddressNumber", Lazy = FetchWhen.OnInvoke)]
        public virtual Vehicle Vehicle { get; set; }

        [BelongsTo("IPAddressNumber", Lazy = FetchWhen.OnInvoke)]
        public virtual JBusConfiguration JBusConfiguration { get; set; }
}

Any help would be greatly appreciated...

Justin
  • 17,670
  • 38
  • 132
  • 201
  • this looks like a DB design problem... are you using a table just to store IPs? – Mauricio Scheffer Feb 23 '11 at 21:01
  • No, I removed the other properties of DeviceConfiguration just to show the relevant associations. Having 2 child tables that both reference a parent table based on its PK doesn't seem like a db design problem to me... – Justin Feb 23 '11 at 21:32
  • If this were direct NHibernate, this is something that would use the configuration, where you have a 1:1 relationship between the parent and child tables, right? Only question is how ActiveRecord supports 1:1 relationships. – Rich Feb 23 '11 at 22:52
  • BelongsTo is meant for 1:1 relationships. – Justin Feb 24 '11 at 15:37
  • possible duplicate of ["Invalid Index n for this SqlParameterCollection with Count=n" OR "foreign key cannot be null"](http://stackoverflow.com/questions/4888089/invalid-index-n-for-this-sqlparametercollection-with-count-n-or-foreign-key-ca) – Dan Aug 31 '11 at 11:51
  • Doesn't seem like a duplicate to me, they are using an xml mapping file, I'm using a code approach in conjunction with Active Record. Also mine is related to a foreign key, theirs was a mistake where they had the pk defined twice. – Justin Sep 29 '11 at 18:34

1 Answers1

1

I ended up having to just remove the second association altogether to get around this issue. Not a great solution but the only one I could find.

Justin
  • 17,670
  • 38
  • 132
  • 201
  • That's indeed not a good solution. I'm having the same problem, which is new to NHibernate 3, since I've had the same mapping since early versions of NHibernate 2 and haven't needed to change this until a recent upgrade to NHibernate 3. – Asbjørn Ulsberg Sep 15 '11 at 16:02