ArgumentOutOfRangeException : Index was out of range. Fluent NHibernate commit

I came across the error

ArgumentOutOfRangeException : Index was out of range.

while trying to perform a commit on a single entity with 4 nested entities and 3 properties.

I tracked the problem down to one two entities: Country and “Post” which I will refer to as Location.

The objects looked like:

public class Location
{
...
        // public virtual string ContactCountry { get; set; }
...
        public virtual Country Country { get; set; }
}

public class Country
{
...       
        public virtual IList<Location> Locations { get; set; }
}

The problem came from the Fluent NHibernate mapping referring to a property on the COUNTRY_CODE field and specifying a HasMany relationship to Country on the same column.

This is an oddity, for sure. But, the better (object-oriented and proper) way to do it would be to change ContactCountry to a getter and return the Country.Code property (if ContactCountry is used elsewhere). Or, call Location.Country.Code only.

Removing the property and mapping for ContactCountry fixed the problem.

Flattr this!