Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate: Index was out of range exception with ReferencesAny mapping
    text
    copied!<p>I'm trying to add a new record to database via creating a new object :</p> <pre><code>using (var session = conn.OpenNewSession()) { using (var tran = session.BeginTransaction()) { TableHours hours = new TableHours(periodId, level, levelId.ToString(), levelTblRef); hours.WorkHours = 10; session.SaveOrUpdate(hours); tran.Commit(); //Exception thrown here } } </code></pre> <p>System.ArgumentOutOfRangeException is thrown after transaction.Commit() : "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"</p> <p>The error as I understand happens because of complicated mapping:</p> <pre><code>public class TableHours { public virtual int SaleMonthId { get; protected set; } public virtual int Level { get; protected set; } public virtual string LevelId { get; protected set; } public virtual Level Lvl { get; protected set; } public virtual decimal WorkHours { get; set; } //..other methods public TableHours(int saleMonthId, int level, string levelId, Level lvl) { this.SaleMonthId = saleMonthId; this.Level = level; this.LevelId = levelId; this.Lvl = lvl; } } </code></pre> <p>And the mapping:</p> <pre><code>public class TableHoursMap : ClassMap&lt;TableHours&gt; { public TableHoursMap() { Table("TableHours"); CompositeId() .KeyProperty(x =&gt; x.SaleMonthId) .KeyProperty(x =&gt; x.Level) .KeyProperty(x =&gt; x.LevelId, "Id"); Map(x =&gt; x.SaleMonthId); Map(x =&gt; x.Level); Map(x =&gt; x.LevelId, "Id"); Map(x =&gt; x.WorkHours); ReferencesAny(x =&gt; x.Lvl) .IdentityType&lt;string&gt;() .EntityTypeColumn("Level") .EntityIdentifierColumn("Id") .AddMetaValue&lt;Level5&gt;("5") .AddMetaValue&lt;Level4&gt;("4") .Not.Insert() .Not.Update() .Cascade.None() .ReadOnly(); } } </code></pre> <p>I.e. Lvl field reference to table Level4 or Level5 table depending on Level value (4 or 5). The select works perfect. But when I'm trying to SaveOrUpdate I have the error above.</p> <p>And small note. Without next attributes:</p> <pre><code>.Not.Insert() .Not.Update() .Cascade.None() .ReadOnly(); </code></pre> <p>I have Index out of range error referenced to Lvl field. So that's why I guess this is the one who blame.</p> <p>The Level4 and Level5 classes inherit from Level class.</p> <p>What am I doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload