views:

30

answers:

2

I have a EDM (phoneDB) that models a back-end MSSQL database. I've developed a ASP.NET (VB) application that allows one to edit the information in this database. When someone edits a record entry I'd like to record this action.

Right now, I'm doing the following:

For Each..Next that checks whether entry is an object that has had its entitystate modified.

And If Not..End If that ensures we aren't dealing with a relationship entity or a null entity.

Now this is where it gets fuzzy. What I want to do is grab the information from these modified objects and record them into the database. Now I have something like this:

Dim audit as History
audit.action = "Changed information in " & propName & " to " & entry.CurrentValues(propName) & " from " & entry.OriginalValues(propName)
audit.action_by = this_user
audit.action_date = Date.Now
audit.extension_id =

I'm not sure, however, how to tell it to pull a specific property from entry. For example, I need to get (pseudo-code) something like:

audit.extension_id = entry.OriginalValues(extension_id)
A: 

I don't understand what do you mean by "pulling a specific property from an entry"? The (pseudo) code you wrote is not telling much, what is an extesion_id in your case? If extension_id is a property name of an entity, then you obtain it's original value by calling entry.OriginalValues("extension_id"), but I'm fairly sure you knew that.

Btw, you can do intricate history recording in the DB itself using triggers without the data layer even knowing it. It's a fairly old trick and works fast, see this

Boris B.
A: 

Here is how I accomplished it in the end:

  Private Shared Sub context_SavingChanges(ByVal sender As Object, ByVal e As EventArgs)
        ' This allows us to record a history of the changes made by the user to the database. The record is created automatically by EF, but we need to save it to the database
        ' for permanent retention.
        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 = CStr(HttpContext.Current.Session("person_name"))
                    audit.action_date = Date.Now
                    audit.extension_id = entry.CurrentValues.GetValue(0)
                    context.AddToHistories(audit)
                    context.SaveChanges()
                Next

            End If

        Next
    End Sub
davemackey