tags:

views:

62

answers:

2

How can I determine which fields have changed after model has been edited

+1  A: 

If you are using Linq to SQL, the GetModifiedMembers method takes an argument of tybe Object, and returns an array of System.Data.Linq.ModifiedMemberInfo objects. Every Table class in the DataContext has a GetModifiedMembers method that can be invoked on any entity.

http://msdn.microsoft.com/en-us/library/system.data.linq.itable.getmodifiedmembers.aspx

In the Entity Framework, using ObjectStateManager, one can access all this change-information like object-state (added/modified/deleted), modified properties, original and current values

IEnumerable<ObjectStateEntry> changes = 
        this.ObjectStateManager.GetObjectStateEntries(
        EntityState.Added | EntityState.Deleted | EntityState.Modified);
Robert Harvey
+1  A: 

You could implement INotifyPropertyChanged on your entities. You would need fire off the PropertyChanged event for each property...so there is some refactoring you would have to do to get this working. Its the only built-in way to achieve it with .NET.

If you don't want to implement INotifyPropertyChanged manually, you could use PostSharp to update your classes and adjust your properties at compile time. However, this would require a considerably more complicated effort up front.

jrista