NHibernate应该如何更新映射为Version的属性 [英] How should NHibernate update properties mapped as Version

查看:141
本文介绍了NHibernate应该如何更新映射为Version的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用流畅的NHibernate,我在使用Version

Using fluent NHibernate I have a property on a class mapped using Version

Version(x => x.Version);

当我保存对象时,数据库中的Version属性会按我期望的那样增加,但是对象上的属性值有时似乎只是在改变.

When I save the object, the Version property gets incremented in the database as I would expect, but the value of the property on the object only seems to change sometimes.

using (var tx = session.BeginTransaction())
{
    session.Merge(item);
    tx.Commit();

    item.Version;  // Sometimes this is still 1, when I expect it to be 2.
}

然后的问题是,如果它保持为1,然后进行更多更改并再次保存,则会收到StaleObjectStateException.

The problem is then that if it remains as 1 and I make more changes and save again I get a StaleObjectStateException.

奇怪的是,有时它可以正常工作,并且item.Version值确实正确地增加了,但是我无法弄清楚有和没有的情况之间的区别.

What's weird is that sometimes it works fine and the item.Version value does get correctly incremented, but I can't figure out the difference between the cases where it does and the cases where it doesn't.

我尝试搜索,但是似乎找不到任何文档.谁能用版本映射说明NHibernates预期的行为是什么?

I've tried searching but can't seem to find any documentation on this. Can anyone explain what NHibernates expected behaviour is with the Version mapping?

[NHibernate版本2.1.2]

[NHibernate version 2.1.2]

推荐答案

您尝试过

item = session.Merge(item);
tx.Commit();

?

这篇关于NHibernate应该如何更新映射为Version的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆