插入到具有而不是来自实体数据框架的触发器的表时出错
我正在使用实体框架4,在使用实体框架在具有替代插入触发器的表中插入新记录时,当表具有标识列时,替代触发器用于根据特定逻辑修改插入的值之一,实体框架引发异常“存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体以来实体可能已被修改或删除。刷新 ObjectStateManager 条目”。
任何人都可以帮助如何解决此异常吗?
I'm using entity framework 4 , on inserting a new record using entity framework in a table that have instead of insert trigger while the table has an identity column , the instead of trigger is used to modify one of the inserted value according to certain logic ,Entity framework raises exception "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries".
Can any one help how to get around this exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Entity Framework 4.1,Ladislav 发布的解决方案将 Scope_Identity() 的 Select 添加到触发器主体的末尾,为我解决了这个问题。为了完整起见,我已在此处复制了整个触发器创建过程。通过这个触发器定义,我可以使用 context.SaveChanges() 将行添加到表中。
编辑处理计算值(感谢评论中的 Chris Morgan):
如果表中有任何其他计算值,则也必须将它们包含在 SELECT 中。例如,如果您有一个使用
GETDATE()
的CreatedDate
列,您将进行如下选择:Using Entity Framework 4.1, the solution posted by Ladislav to add a Select of Scope_Identity() to the end of the trigger body solved the problem for me. I have copied the entire trigger creation here for completeness. With this trigger defenition I was able to add rows to the table using context.SaveChanges().
Edit for handling computed values (Thanks to Chris Morgan in the comments):
If you have any other computed values in the table you will have to include them in the SELECT as well. For example if you had a
CreatedDate
column that usesGETDATE()
you would make the select like this:而不是执行触发器,而不是实体框架创建的插入操作。这可能是潜在的问题,因为一旦您使用标识列,每个插入后面都会跟着:
所以问题是一旦插入被替换,这个查询会发生什么。如果执行并返回 null,则会出现异常。您可以在触发器中插入记录后添加它,但如果原始查询也被执行,它将无济于事。
您可以将触发器更改为在插入和修改数据之前或之后。
Instead of trigger is executed instead of Insert operation created by Entity framework. This can be potential problem because once you are using identity column each insert is followed by:
So the question is what happens with this query once insert is replaced. If it is executed and return null you get and exception. You can add it after you insert record in your trigger but it will not help if the original query is executed as well.
You can change your trigger to be either before or after insert and modify data.
您还需要返回标记为已计算的所有属性
You also need to return any properties marked as Computed
我还发现您需要将 StoreGeneratePattern 设置为 Identity 才能使其在我用作主键但未生成标识值的 nvarchar 列上工作。这是针对插入后触发器的情况,该触发器计算出要存储在键列中的唯一值。在其他情况下(添加和更新),可能需要将其设置为“已计算”。
I also found that you need to have the StoreGeneratedPattern set to Identity to get it to work on a nvarchar column that I was using as a primary key but which was not generating an identity value. This was for situations of an after insert trigger which computed a unique value to store in the key column. In other situations (add and update), it may require being set to Computed.