Powerslave
Powerslave

Reputation: 457

NHibernate does not update Index-column when removing an object from a list

My class A has a property:

public virtual IList<IVisitor> Visitors { get; set; }

which is mapped as follows:

<list name="Visitors" table="Visitor" cascade="all-delete-orphan">
  <key column="SomeID" not-null="true" update="false" />
  <index column="idx" />
  <one-to-many class="Visitor" />
</list>

When there are 3 objects in the list, they have the idx 0, 1, and 2.

Now when I remove the middle object and save my instance of A like:

a.Visitors.RemoveAt( 1 );
a.Save(); // calls session.SaveOrUpdate( a );

the appropriate row is deleted but the other two rows keep its idx 0 and 2.

Why is NHibernate not updating it to 0 and 1?

Thank you.

Upvotes: 1

Views: 278

Answers (1)

Fr&#233;d&#233;ric
Fr&#233;d&#233;ric

Reputation: 9854

Indexes are not updated because your mapping mandates them not to be updated.

Remove update="false" from your key mapping. It does apply to index too. The index is considered part of the list key.

If you use an old version of NHibernate, you may additionally have this old bug.

Upvotes: 1

Related Questions