I have an entity with a surrogate Id and a composite NaturalId mapped with FluentNHibernate. I make the natural id mutable marking it "Not.ReadOnly()". Something like:
public class EntityMap: ClassMap<Entity>
{
public EntityMap()
{
Id(x => x.Id);
NaturalId().Not.ReadOnly()
.Reference(x => x.OtherEntity, "OtherEntityId")
.Property(x => x.IntegerProperty);
// more fields
}
}
The generated xml is like:
<natural-id mutable="true">
<property name="IntegerProperty" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="IntegerProperty" />
</property>
<many-to-one class="OtherEntity, OtherEntity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="OtherEntity">
<column name="OtherEntityId" />
</many-to-one>
</natural-id>
If I change OtherEntity, the operation works fine and the entity is updated in the database. If I change IntegerPropery, I'm getting and exception: "immutable natural identifier of an instance of Namespace.Entity was altered".
Why is it complaining about the "immutable natural identifier" if it is marked as mutable="true"?
The code is something like:
using (var session = SessionManager.OpenSession())
using (var tran = session.BeginTransaction())
{
session.Update(entity);
entity.IntegerProperty = (int)differentValue;
tran.Commit();
}
Thanks