实体框架:观察对象上保存的更改
对于我的项目,我必须通过实体框架记录对我的对象所做的所有更改。这仅包括注册何时在哪个表上编辑了哪些字段。
粗略地说,将更改放入具有这种结构的表中: IDEvent、EventDate、TableName、RowID、FieldName、OldValue、NewValue
如果有多个更改,将插入多行。
它已经适用于我的 90% 的情况,我正在侦听 ObjectContext 的 SavingChanges 事件
我唯一的问题:在添加的情况下,由 SQL(IDENTITY) 生成的主键此时不存在(逻辑)在 SavingChanges 事件上,因为它还没有存储在数据库中,问题是我真的需要它(在我的表中填充我的 RowID)
那么,你知道如何做到这一点吗?我没有找到任何“ChangesSaved”事件。解决方法的想法?
For my project, I have to log all changes made on my objects, through the entity framework. This consists just to register which fields have been edited on which table at which time.
Roughly, put changes in a table with this kind of structure:
IDEvent, EventDate, TableName, RowID, FieldName, OldValue, NewValue
If there is multiple changes, several rows will be inserted.
It already works for 90% of my cases, I'm listening the SavingChanges event of the ObjectContext
My only problem: In the case of an add, my primary keys that are generated by SQL(IDENTITY), are not present at this moment(logic) on the SavingChanges event, because it's not already stored in the DB, and the problem is that I really need it(To fill my RowID in my table)
So, do you have an idea how to do this? I didn't found any "ChangesSaved" event. An idea of workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将无法在 SavingChanges 事件中执行此操作。我认为您可以为 ObjectContext 创建自己的包装器,并在 SaveChanges 的包装器方法中实现您自己的逻辑。逻辑应该类似于
您还应该将 TransactionScope 添加到新的 SaveChanges 中。
You will not be able to do this in SavingChanges event. I think you can create your own wrapper for ObjectContext and implement your own logic in wrapper method for SaveChanges. Logic should be like
You should also add TransactionScope to your new SaveChanges.