WPF and EF6 mark model entity as having chnages

.net entity-framework entity-framework-6 vb.net wpf


Background: With MVVM (MVVM-Light) and Entity Framework 6 db initially, I have a WPF 4.5 application.

In a few of my viewmodels, the list/details setup consists of a list of objects in a listbox and a details grid that shows and lets the user edit the selected record from the listbox. When changes are made to the underlying model entity of the chosen record, I want the SAVE and UNDO buttons at the top of the grid to become "active."

I can accomplish this, but my present method performs horribly, thus I need a different approach to track down model changes.

What I Currently Have

I have a CanSaveExecute method that is an ICommand Object's CanExecute Callback. I check the EF dbContext ChangeTracker Entities in that procedure to see if any objects of the type I'm looking for are there.

    If _Selection IsNot Nothing AndAlso _Selection.HasErrors = False Then
         Return (From entry In Context.ChangeTracker.Entries(Of job)() Where entry.Entity.idJob = _Selection.idJob And entry.State = EntityState.Modified Select entry).Count
         Return False
    End If
 Catch ex As Exception
    Return False
 End Try

The UI speed is being ruined by this query of the change tracker entries, according to the issue. With human input, it seriously lags.

My inquiry:

Has anyone come up with a more effective way for using Entity Framework 6 to find changes in a CanExecute function? Without modifying the T4 code, if possible (but I have a feeling this is where I will end up).

Greater Detail

This is how my model is configured. For me, EF6 creates a straightforward entity class that appears as follows: (As an example, I eliminated a lot of properties to keep it straightforward)

Imports System
Imports System.Collections.Generic

Partial Public Class job
    Public Property idJob As Integer
    Public Property idLinkedJob As Nullable(Of Integer)
    Public Property idStatus As Byte
    Public Property idEstimate As Nullable(Of Integer)
    Public Property chrTitle As String

    Public Overridable Property alerts As ICollection(Of alert) = New HashSet(Of alert)
    Public Overridable Property client As client
End Class

I add more datavalidation rules to that class by extending it with another partial class, like this one: (Again, a simplified illustration)

Partial Public Class job
    Inherits ValidationBase

    Public Property HasChanges As Boolean = False
#End Region

    Public Sub New()
        ''default values
        Me.FTC_Type = 4
        Me.dtCreated = Now
        Me.dtUpdated = Now
        'HasChanges = False
    End Sub

    Public ReadOnly Property DisplayPath
            Return "W" + idJob.ToString + ": " + chrTitle + " - " + client.chrCompany
        End Get
    End Property
#End Region


    Public Overrides Function Validate(validationContext As ComponentModel.DataAnnotations.ValidationContext) As IEnumerable(Of ComponentModel.DataAnnotations.ValidationResult)
        Return MyBase.Validate(validationContext)
    End Function

    Public Sub PropertyValitaion(bAllProperties As Boolean, Optional sProperty As String = "")
        'initialize validation helper

        If bAllProperties OrElse sProperty = "chrTitle" Then
            If String.IsNullOrEmpty(chrTitle) Then
                AddError("chrTitle", "You must enter a Job Title")
            End If
        End If
        If bAllProperties OrElse sProperty = "idClient" Then
            If idClient < 1 Then
                AddError("idClient", "You must select a job client")
            End If
        End If

        If String.IsNullOrEmpty(sProperty) = False Then
        End If

    End Sub
#End Region
End Class

No answer:

So, after debating it for a day, I've come to the conclusion that @Shoe is correct. For this functionality, it turned out to be too much work. I was never able to get the change tracker to promise to stop causing UI latency.

11/23/2013 5:10:14 PM

Popular Answer

12/5/2013 5:25:05 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow