开发者

WCF RIA's HasChanges for EntityFramework

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 HasChan开发者_Go百科ges 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?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜