nhibernate审计触发错误
我在 sql 数据库上使用触发器来捕获表的更改信息,但 nhibernate 似乎有问题。
该表有一些列、主键和触发器。触发器看起来像这样
CREATE TRIGGER [dbo].[tr_Instrument_update] ON [dbo].[Instrument] FOR UPDATE AS
BEGIN
INSERT [MyAudit].[audit].[Instrument]
SELECT 'Updated', i.*
FROM inserted
INNER JOIN [MyAudit].[dbo].[Instrument] i ON inserted.[InstrumentID] = i.[InstrumentID]
END
基本上在每次更改时我们都会将行复制到审核表中。我已经测试过,如果我直接通过 SQL Management Studio 修改数据,触发器功能会正确,并且我会将数据写入审核表,但是如果我通过我的应用程序更新,我会得到以下信息:
NHibernate.StaleObjectStateException 未被用户代码处理
消息 = 行已更新或删除 另一笔交易(或未保存的值 映射不正确)
我认为这是因为触发器更新了另一个数据库中的另一个表,无论如何都可以让 nhibernate 忽略这一点,因为更改不会影响其任何数据,在我们的映射中我们没有引用此审计数据。
I'm using triggers on my sql database to capture change information for a table, it seems to be having a problem with nhibernate though.
The table has a few columns and primary keys and triggers on it. The triggers look like this
CREATE TRIGGER [dbo].[tr_Instrument_update] ON [dbo].[Instrument] FOR UPDATE AS
BEGIN
INSERT [MyAudit].[audit].[Instrument]
SELECT 'Updated', i.*
FROM inserted
INNER JOIN [MyAudit].[dbo].[Instrument] i ON inserted.[InstrumentID] = i.[InstrumentID]
END
Basically on every change we copy the row into the audit table. I have tested and if I modify the data directly through sql management studio triggers function correctly and I get data written to the audit table, however if i update through my app I get the following:
NHibernate.StaleObjectStateException
was unhandled by user code
Message=Row was updated or deleted by
another transaction (or unsaved-value
mapping was incorrect)
I assume this is because the trigger updates another table in another database, is there anyway to make nhibernate ignore this as the change will not affect any of its data, in our mappings we have no reference to this audit data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现触发器由于某种原因导致 Nhibernate 执行两次相同的更新调用。解决方案是在触发器内设置 NOCOUNT ON。仍然不确定为什么 nhibernate 会进行两次更新!
Figured out that the trigger was causing Nhibernate to do two identical update calls for some reason. The solution was to SET NOCOUNT ON inside the trigger. Still not sure though why nhibernate makes two updates!