FluentNHibernate 中的 Mutable NaturalId 修改时会抛出异常
我有一个带有代理 ID 的实体和一个使用 FluentNHibernate 映射的复合 NaturalId。我使自然 id 可变,将其标记为“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 实例的不可变自然标识符已更改”。
如果它被标记为 mutable="true",为什么它会抱怨“不可变的自然标识符”?
代码是这样的:
using (var session = SessionManager.OpenSession())
using (var tran = session.BeginTransaction())
{
session.Update(entity);
entity.IntegerProperty = (int)differentValue;
tran.Commit();
}
谢谢
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 get the 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,属性为
.Not.ReadOnly
。它是自然 ID,默认情况下是不可变的。
对于 XML 映射,您必须指定
mutable="true"
。在 Fluent 中寻找类似的东西(我不确定是否支持)Properties are
.Not.ReadOnly
by default.It's the natural id which is non-mutable by default.
With XML mappings, you'd have to specify
mutable="true"
. Look for something similar in Fluent (I'm not sure if it's supported)