检查 SavingChanges 上正在更新哪些实体

发布于 2024-11-03 15:03:17 字数 640 浏览 0 评论 0原文

我有一个处理程序,当对实体对象之一进行更新时,它会导致执行子 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2024-11-10 15:03:17

您可以检查 ObjectStateEntry.Entity 的类型[msdn]

If TypeOf entry.Entity Is Phone Then
  ' ...
End If

You can check for the type of the ObjectStateEntry.Entity [msdn]:

If TypeOf entry.Entity Is Phone Then
  ' ...
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文