views:

39

answers:

1

Here is my code:

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

End Class

How do I save this new object I've created? In LINQ I'd do something like:

datasource.object.insertonsubmit(audit)
datasource.SubmitChanges()
A: 

Here is my final code:

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

Essentially, what I had to do was:

  • define within my ForEach a new instance of AppsEntities ("context"),
  • then I had to add the audit object to histories in the present AppEntities ("context").
  • Finally, I had to tell context ("AppsEntities") to save the changes to the database (stop just holding it in the EF cloud).
davemackey
Is this working for you? You might get a (wait for it....) *stack overflow* with that code. You have a SavingChanges handler that calls SaveChanges(). Well, calling SaveChanges() fires the SavingChanges event, which will call your handler, which will call SaveChanges() which will....stack overflow. Just take the call to SaveChanges() out. That should be done from your calling site.
Samuel Meacham
It doesn't add it if I don't have the context.SaveChanges() in there. It does work for me like this. I'm guessing perhaps that Me.SavingChanges is a different instantiation than context.SaveChanges?
davemackey

related questions