【问题标题】:Entity Framework - Original value the same as current value实体框架 - 原始值与当前值相同
【发布时间】:2016-05-05 08:37:02
【问题描述】:

我最初有这个工作,但它似乎已经停止工作。 originalValue 和 currentValue 的值似乎总是相同的。

这是我的代码

Public Overrides Function SaveChanges() As Integer

    Dim modifiedEntities = ChangeTracker.Entries.ToList

    For Each ent As DbEntityEntry In modifiedEntities
        If ent.State = EntityState.Modified Then
            Dim type = ent.Entity.GetType.Name
            Dim primaryKey = GetPrimaryKeyValue(ent)

            Dim jo As New JObject
            For Each prop In ent.OriginalValues.PropertyNames
                Dim originalValue = ent.OriginalValues(prop).ToString
                Dim currentValue = ent.CurrentValues(prop).ToString
                If originalValue <> currentValue Then
                    jo.Add(New JProperty(prop, currentValue))
                End If
            Next
        End If
    Next

    Return MyBase.SaveChanges()

End Function

我知道 JSON 对象实际上并没有做任何事情,但是在单步执行代码时,即使值已更新,originalValue 也始终与 currentValue 相同。

【问题讨论】:

    标签: .net vb.net entity-framework


    【解决方案1】:

    你可以试试这个:
    How to get original values of an entity in Entity Framework?

    Dim originalValue = CType(context.ObjectStateManager.GetObjectStateEntry(ent).OriginalValues(prop), String)
    

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多