检查在SavingChanges上更新的实体

时间:2011-04-26 20:18:29

标签: asp.net vb.net entity-framework entity-framework-4 linq-to-entities

我有一个处理程序,当对其中一个实体对象进行更新时会导致执行子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

1 个答案:

答案 0 :(得分:3)

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

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