0

这是我的代码:

Partial Public Class AppsEntities
Private Sub OnContextCreated()
    AddHandler Me.SavingChanges, AddressOf context_SavingChanges
End Sub
Private Shared Sub context_SavingChanges(ByVal sender As Object, ByVal e As EventArgs)
    For Each entry As ObjectStateEntry In DirectCast(sender, ObjectContext).ObjectStateManager.GetObjectStateEntries(EntityState.Modified)
        If Not entry.IsRelationship And entry.Entity IsNot Nothing Then
            For Each propName As String In entry.GetModifiedProperties()
                Dim audit As New History
                audit.action = "Changed information in " & propName & " to " & entry.CurrentValues(propName) & " from " & entry.OriginalValues(propName)
                audit.action_by = "dmackey"
                audit.action_date = Date.Now
                audit.extension_id = entry.CurrentValues.GetValue(1)
            Next

        End If


    Next
End Sub

结束类

如何保存我创建的这个新对象?在 LINQ 中,我会做类似的事情:

datasource.object.insertonsubmit(audit)
datasource.SubmitChanges()
4

1 回答 1

0

这是我的最终代码:

Partial Public Class AppsEntities
Private Sub OnContextCreated()
    AddHandler Me.SavingChanges, AddressOf context_SavingChanges
End Sub
Private Shared Sub context_SavingChanges(ByVal sender As Object, ByVal e As EventArgs)
    For Each entry As ObjectStateEntry In DirectCast(sender, ObjectContext).ObjectStateManager.GetObjectStateEntries(EntityState.Modified)
        If Not entry.IsRelationship And entry.Entity IsNot Nothing Then
            For Each propName As String In entry.GetModifiedProperties()
                Dim context As New AppsEntities()
                Dim audit As New History
                audit.action_note = "Changed information in " & propName & " to " & entry.CurrentValues(propName) & " from " & entry.OriginalValues(propName)
                audit.action_by = "dmackey"
                audit.action_date = Date.Now
                audit.extension_id = entry.CurrentValues.GetValue(0)
                context.AddToHistories(audit)
                context.SaveChanges()
            Next

        End If

    Next
End Sub

End Class

本质上,我必须做的是:

  • 在我的 ForEach 中定义一个新的 AppsEntities 实例(“上下文”),
  • 然后我必须将审计对象添加到当前 AppEntities(“上下文”)中的历史记录中。
  • 最后,我必须告诉上下文(“AppsEntities”)以将更改保存到数据库(停止仅将其保存在 EF 云中)。
于 2010-07-01T17:20:44.047 回答