2
votes

It seems like supporting Entity.IsDirty should be easy with Entity Framework 4.1 with Automatic Change Tracking (proxy objects) turned on. However, I have not found this to be the case so far.

public class DomainContext : DbContext, IDomainContext
{

    /// <summary>
    /// Indicates whether changes have been made to the entity with that have 
    /// not been saved. This method assumes EF Proxies are being used for change tracking.
    /// </summary>
    /// <param name="entityId">The Id of the entity.</param>
    /// <returns>True if the entity is dirty, false otherwise.</returns>
    public bool HasChanges(Guid entityId)
    {
        foreach (DbEntityEntry entry in this.ChangeTracker.Entries())
        {
            IEntity entity = entry.Entity as IEntity;
            if (entity != null)
            {
                if (entity.Id == entityId && entry.State != System.Data.EntityState.Unchanged)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

The above method doesn't seem to work when changes have been made to properties on the entity that are reference/complex properties. For instance:

public class UserPreferences : EntityBase
{

    /// <summary>
    /// Comma delimited list of AFEs for which the user is interested in viewing Events.
    /// </summary>
    public virtual string ViewableAFEs { get; set; }

    /// <summary>
    /// The primary operating area for the user. This is used for defaulting purposes.
    /// </summary>
    public virtual OperatingArea PrimaryOperatingArea
    {
        get { return _primaryOpArea; }
        set
        {
            if (_primaryOpArea != value)
            {
                _primaryOpArea = value;
                RaisePropertyChanged(() => PrimaryOperatingArea);
            }
        }
    }
}

If I new up the above class, or get an existing UserPreferences entity from the database and then change the PrimaryOperatingArea property, DomainContext.HasChanges will return false. I believe this is occuring because Entity Framework tracks complex & reference properties differently from value type properties.

Changing the ViewableAFEs property (string) does show up as a change when calling the HasChanges method above.

My question is how do I expose a generic method on my derived DomainContext that will evaluate all properties (including complex, reference, collection types) and determine if the entity is dirty?

I'm curious, have others of you using EF 4.1 leveraged EF for IsDirty functionality, or did you roll your own isDirty by using INotifyPropertyChanged, etc. ?

Thanks!

1

1 Answers

3
votes

DbContext API already exposes options to check if an entity or some properties were changed.

  • to check if an entity was modified:

    var myFooHasChanges = context.Entry(myFoo).State == EntityState.Modified;

  • to check if a property was modified:

    var barWasModified = context.Entry(myFoo).Property(u => u.Bar).IsModified;

You can read all about this in: Using DbContext in EF 4.1 Part 5: Working with Property Values