检查 SavingChanges 上正在更新哪些实体
我有一个处理程序,当对实体对象之一进行更新时,它会导致执行子 context_SaviningChanges,如下所示:
AddHandler Me.SavingChanges, AddressOf context_SavingChanges
我希望为每个正在更新的实体执行特定的代码。因此,如果它是“电话”实体,我想触发一段代码,但如果它是“建筑物”实体,我想触发另一段代码。在伪代码中我想做这样的事情:
For Each entry as ObjectStateEntry in DirectCast(sender, ObjectContext).ObjectStateManager.GetObjectStateEntries(EntityState.Modified)
If entry.entity("phone") Then
... code goes here for phone changes ...
ElseIf entry.entity("building")
... code goes here for building changes ...
Else
... code goes here for other entity changes ...
Next
I have a handler that causes a sub context_SaviningChanges to be executed when an update is made to one of the entity objects like so:
AddHandler Me.SavingChanges, AddressOf context_SavingChanges
I want to have specific code executed for each entity that is being updated. So, if it is a "phone" entity I want to trigger one piece of code but if it is a "buildings" entity I want to trigger another piece of code. In pseudo code I want to do something like this:
For Each entry as ObjectStateEntry in DirectCast(sender, ObjectContext).ObjectStateManager.GetObjectStateEntries(EntityState.Modified)
If entry.entity("phone") Then
... code goes here for phone changes ...
ElseIf entry.entity("building")
... code goes here for building changes ...
Else
... code goes here for other entity changes ...
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查 ObjectStateEntry.Entity 的类型[msdn]:
You can check for the type of the ObjectStateEntry.Entity [msdn]: