4

I'm currently converting some code over from Silverlight/WCF RIA Services to WPF/Entity Framework. The codebase made extensive use of the HasChanges property of the RIA Domain Context. The view would bind to this property to determine button states. For example, a form would bind to this HasChanges property, and whenever the user changed any property of any entity inside the DomainContext, the HasChanges would become true and the Save and Discard buttons would become enabled.

After doing some research, it is apparent that EF does not have an equivalent HasChanges property on the ObjectContext. Does anyone have any clever ideas on how one would go about duplicating this functionality inside of Entity Framework?

I suppose these would be the important features for such a property:

  1. This new HasChanges property would become true whenever any property of any entity loaded into the ObjectContext changes.
  2. The HasChanges would become false whenever the SaveChanges method is successfully called on the ObjectContext.
  3. The HasChanges property throws a PropertyChanged event which the view would catch in order to update button states / etc.

Anybody have any ideas? Maybe a custom ADO.NET EntityObject Generator?

Andrew Garrison
  • 6,977
  • 11
  • 50
  • 77
  • If anybody is interested, Rune has a good idea on how to accomplish this at this link: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/9154e9ca-8a02-4eb1-9707-153132c3f3a1 – Andrew Garrison Jul 07 '11 at 15:23

1 Answers1

0

I had the same issue. The solution isn't quite ideal, but it works. Also note that I'm using Self Tracking Entities within a WPF application.

To check for changes

  1. Create an instance of the context
  2. ApplyChanges() from the model to the context
  3. Check context.HasChanges()

    using (var context = new MyEntities())
    {
        context.MyTable.ApplyChanges(myTableInstance);
        return context.HasChanges();
    }

This works well, even for a graph of objects.

NathanAW
  • 3,329
  • 18
  • 19
  • Thanks for the input, however this solution is lacking the features of the RIA HasChanges in that no event is fired when a property on an entity is changed. In your solution, it appears that you have to 'poll' the context and ask if there are any changes. I guess what I'm after is one event I can subscribe to on the Context that is fired whenever any entity's property is changed in the graph. – Andrew Garrison Jun 21 '11 at 19:01
  • 1
    What type of entities are you using? Standard EF entities, POCOs, or Self Tracking? If you're using POCO or Self Tracking, then one option would be to modify the T4 template to raise such an event. – NathanAW Jul 01 '11 at 18:32