Fluent NHibernate 新手:行已被另一个事务更新或删除
我正处于使用 Fluent NHibernate 构建数据库的早期阶段。我在 ASP.NET MVC 3 中实现了一个工作单元模式,让 NHibernate 为我更新我的数据库架构。为了插入/更新我的初始数据,我有一个带有 Update
操作的 Database
控制器,该操作尝试 SaveOrUpdate(...)
一个 >User
实体(管理员用户)添加到 Users
表中。
通过 Visual Studio 手动删除所有用户记录并重新运行 Update
操作以重新填充 Users
表后,我收到以下 NHibernate.StaleObjectStateException
例外:
Row was updated or deleted by another transaction (or unsaved-value mapping was
incorrect): [Invoicer.Data.Entities.User#3105248d-ca91-4c64-bf8f-9ebb017943b7]
Line 26: {
Line 27: if (_transaction.IsActive)
Line 28: _transaction.Commit();
Line 29: }
注意:保存或更新的用户 ID 是具有上述值 (3105...) 的 Guid。
我做错了什么?
I'm in the early stages of building out a database with Fluent NHibernate. I implemented a unit-of-work pattern in ASP.NET MVC 3 to let NHibernate update my database schema for me. To insert/update my initial data, I have a Database
controller with an Update
action that tries to SaveOrUpdate(...)
a User
entity (the administrator user) into the Users
table.
After manually deleting all user records via Visual Studio and re-running my Update
action to repopulate the Users
table, I receive the following NHibernate.StaleObjectStateException
exception:
Row was updated or deleted by another transaction (or unsaved-value mapping was
incorrect): [Invoicer.Data.Entities.User#3105248d-ca91-4c64-bf8f-9ebb017943b7]
Line 26: {
Line 27: if (_transaction.IsActive)
Line 28: _transaction.Commit();
Line 29: }
Note: the saved or updated user ID is a Guid with the value above (3105...).
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过映射解决
NHibernate 使用 Guid 梳方法为我分配用户 ID(Guid)。手动指定 ID 会导致映射失败。我更喜欢自己分配这些 ID,因此我将
User
实体 ID 映射更改为在我的UserMap
类中手动分配:Solved with Mapping
NHibernate was assigning User IDs (Guid) for me using the Guid comb method. Manually specifying the ID was tripping up the mapping. I prefer to assign these IDs myself, so I changed the
User
entity ID mapping to be manually assigned in myUserMap
class:如果你PK的是角色,当上下文改变时也会出现这个错误。
解决方案是在
Update(data, data.code.Trim())
; 时在 PK 中应用 Trim();This error also occurs when the changed context and you if you PK is character.
The Solution is apply Trim() in PK when
Update(data, data.code.Trim())
;