Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid index n for this SqlParameterCollection with Count=n

NHibernate throws an Exception: "Invalid index n for this SqlParameterCollection with Count=n." when I attempt to save a Meter object. The problem is with the CurrentReading property. If I comment that mapping out, all is well. Also if I manually set the CurrentReading in the database NHibernate queries it fine.

I'm relatively new to NHibernate, any help would be greatly appreciated.

I have the following classes:

public class Meter
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }

    public virtual MeterReading CurrentReading { get; protected set; }

    private IList<MeterReading> _readings;
    public virtual ReadOnlyCollection<MeterReading> Readings
    {
        get { return new ReadOnlyCollection<MeterReading>(_readings); }
    }

    public virtual void AddMeterReading(MeterReading MeterReading)
    {
        _readings.Add(MeterReading);
        if (CurrentReading == null || MeterReading.ReadingTimestamp > CurrentReading.ReadingTimestamp)
        {
            CurrentReading = MeterReading;
        }
    }
}

public class MeterReading
{
    public virtual Guid Id { get; set; }
    public virtual decimal Usage { get; set; }
    public virtual DateTime ReadingTimestamp { get; set; }
}

And this fluent nhibernate mapping file:

public sealed class MeterMap : ClassMap<Meter>
{
    public MeterMap()
    {
        Id(x => x.Id);

        References(m => m.CurrentReading)
            .Column("CurrentMeterReadingId")
            .LazyLoad()
            .Cascade.None()
            .Nullable();

        HasMany(x => x.Readings)
            .KeyColumn("MeterId")
            .Access.CamelCaseField(Prefix.Underscore)
            .LazyLoad()
            .Inverse()
            .Cascade.All();
    }
}

public sealed class MeterReadingMap : ClassMap<MeterReading>
{
    public MeterReadingMap()
    {
        Id(x => x.Id);

        Map(x => x.ReadingTimestamp)
            .Not.Nullable();

        Map(x => x.Usage)
            .Precision(18)
            .Scale(8)
            .Not.Nullable();
    }
}
like image 674
user774031 Avatar asked May 28 '26 20:05

user774031


2 Answers

My guess would be that it has something to do with mapping the same column twice. This is what this error generally means. You might try this alternative mapping since you aren't using cascading on CurrentMeterReading:

References(m => m.CurrentReading)
        .Column("CurrentMeterReadingId")
        .Not.Update()
        .Not.Insert();
        .Nullable();

Also I believe lazy loading is enabled by default so I don't think there is a need to specify that in the mapping.

like image 94
Cole W Avatar answered May 31 '26 16:05

Cole W


I had the same problem. My mistake was; I had an association property(many-to-one) and in my mapping didn't specify insert="false" for that association.

like image 22
sovantha Avatar answered May 31 '26 17:05

sovantha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!