开发者

How to determine if an Entity with relationship properties has changes

Dim myEmployee as Employee = myObjectContext.Employee.Where("it.EmployeeID = 1").First()

The following line will cause e.EntityState to equal EntityS开发者_C百科tate.Modified :

myEmployee.Name = "John"

However, changing a property that is a relationship will leave e.EntityState = EntityState.Unchanged. For example:

myEmployee.Department = myObjectContext.Department.Where("it.DepartmentName = 'Accounting'").First()

How can I tell if myEmployee has changes? I need to know so I can log changes made to the Employee record for auditing purposes.


There is a way to get the state of a relationship, but it is not as easy to obtain as the state of an entity.

ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState state) returns IEnumerable<ObjectStateEntry> with entries for both, entities and relationships (there is IsRelationship property on ObjectStateEntry so you can determinate if it's relationship or entity).

I tested out with with your example when relationship is changed the way you do

myEmployee.Department = myObjectContext.Department.Where("it.DepartmentName = 'Accounting'").First()

and I find out by calling GetObjectStateEntries for each possible EntityState that one ObjectStateEntry is added with state Added:

myObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added)

Now, you can peek at the current values of the state entry to see if they match the ends of the relationship (not nice). However, it's a bit complicated and I'm not sure if it's going to meet your needs in every case.


i was having a similar issue when i was trying to validate in Entity framework: After researching a little bit i found a solution: (see im posting the whole validation solution)

Interface for validation:

Interface IValidatable

Function Validate(Optional ByVal guardando As Boolean = False) As List(Of  ApplicationException)

End Interface

Handling the SavingChanges event in a partial class:

Partial Class FacturacionEntities

Private Sub FacturacionEntities_SavingChanges(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SavingChanges
    Dim objects As New List(Of System.Data.Objects.ObjectStateEntry)
    objects.AddRange(Me.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
    objects.AddRange(Me.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))

    Dim errors As New List(Of ApplicationException)
    For Each obj In objects
        If obj.IsRelationship Then
            Dim fro = DirectCast(obj.CurrentValues(1), EntityKey)
            Dim k As New EntityKey("FacturacionEntities." & fro.EntitySetName, fro.EntityKeyValues(0).Key, fro.EntityKeyValues(0).Value)
            errors.AddRange(DirectCast(Contexto.Facturacion.GetObjectByKey(k), IValidatable).Validate())
        Else
            errors.AddRange(DirectCast(obj.Entity, IValidatable).Validate)
        End If
    Next
    If errors.Count > 0 Then
        Dim err_list As String = ""
        For Each s In errors
            err_list = err_list & s.Message & vbCrLf
        Next
        Throw New ApplicationException(err_list)
    End If
End Sub
  End Class

Please note than "Contexto.Facturacion" is an instance of the Entities class generated by Entity framework engine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜