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:
- This new
HasChanges
property would become true whenever any property of any entity loaded into theObjectContext
changes. - The
HasChanges
would become false whenever theSaveChanges
method is successfully called on theObjectContext
. - The
HasChan开发者_Go百科ges
property throws aPropertyChanged
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
- Create an instance of the context
- ApplyChanges() from the model to the context
- Check context.HasChanges()
using (var context = new MyEntities())
{
context.MyTable.ApplyChanges(myTableInstance);
return context.HasChanges();
}
This works well, even for a graph of objects.
精彩评论