实体框架:创建更改历史记录

发布于 2024-09-08 11:30:56 字数 707 浏览 4 评论 0原文

我有一个 EDM (phoneDB),它对后端 MSSQL 数据库进行建模。我开发了一个 ASP.NET (VB) 应用程序,允许用户编辑该数据库中的信息。当有人编辑记录条目时,我想记录此操作。

现在,我正在执行以下操作:

For Each..Next 检查条目是否是已修改其实体状态的对象。

如果不是..结束如果这确保我们没有处理关系实体或空实体。

现在这就是它变得模糊的地方。我想要做的就是从这些修改的对象中获取信息并将它们记录到数据库中。现在我有这样的事情:

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 =

但是,我不确定如何告诉它从条目中提取特定的属性。例如,我需要获取(伪代码)类似的内容:

audit.extension_id = entry.OriginalValues(extension_id)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

旧竹 2024-09-15 11:30:56

我不明白“从条目中提取特定属性”是什么意思?您编写的(伪)代码并不能说明什么,您的情况下的 extesion_id 是什么?如果extension_id是实体的属性名称,那么您可以通过调用entry.OriginalValues(“extension_id”)来获取它的原始值,但我相当确定您知道这一点。

顺便说一句,您可以使用触发器在数据库本身中进行复杂的历史记录,而数据层甚至不知道这一点。这是一个相当古老的技巧,而且工作速度很快,请参阅 这个

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

浅紫色的梦幻 2024-09-15 11:30:56

这是我最终完成的方法:

  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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文