У меня есть объект с суррогатным Id и составной NaturalId, сопоставленный с FluentNHibernate.Я делаю естественный идентификатор изменяемым, помечая его "Not.ReadOnly ()".Что-то вроде:
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
}
}
Сгенерированный xml похож на:
<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>
Если я изменю OtherEntity, операция будет работать нормально, а сущность обновится в базе данных.Если я изменяю IntegerPropery, я получаю исключение: «неизменный естественный идентификатор экземпляра Namespace.Entity был изменен».
Почему он жалуется на «неизменный естественный идентификатор», если он помечен как изменяемый = »true "?
Код выглядит примерно так:
using (var session = SessionManager.OpenSession())
using (var tran = session.BeginTransaction())
{
session.Update(entity);
entity.IntegerProperty = (int)differentValue;
tran.Commit();
}
Спасибо