2

I am using EF4 repositories in a ASP.NET MVC3/WCF application. I am using the Unit of Work pattern to apply changes to the database

One of the user requirements is to create a ticket/email with a list of changes to the entity. Is there a way I can detect only the changed properties on an entity in the following function?

public void UpdateTrackedEntity<T>(T modifiedEntity) where T : class
{
    var set = CreateObjectSet<T>();
    set.ApplyCurrentValues(modifiedEntity);
}
Pranav Shah
  • 3,233
  • 3
  • 30
  • 47
kolhapuri
  • 1,581
  • 4
  • 20
  • 31

2 Answers2

2

Yes there is a way:

public void UpdateTrackedEntity<T>(T modifiedEntity) where T : class
{
    var set = CreateObjectSet<T>();
    set.ApplyCurrentValues(modifiedEntity);
    var entry = ObjectStateManager.GetObjectStateEntry(modifiedEntity);
    // entry has two collections: CurrentValues (those you applied) and 
    // OriginalValues (those loaded from DB)
    // It also have method GetModifiedProperties to get collection of modified 
    // property names.
}

Check ObjectStateEntry for more details.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • I get the following error when I make the GetObjectStateEntry call, The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type '', T being my Entity type – kolhapuri Apr 27 '11 at 20:28
  • @kolhapuri: Yes that is correct. You must pass the attached instance to the `ObjectStateManager`. Are you using POCOs or EntityObjects? – Ladislav Mrnka Apr 27 '11 at 20:33
  • To give you a little bit more info.. I changed my functions as follows. The originalentity exists in the Objectset. Context.Users.Attach(originalEntity); var entry = Context.UpdateTrackedEntity(modifiedEntity); return entry.GetModifiedProperties(); public ObjectStateEntry UpdateTrackedEntity(T modifiedEntity) where T : class { var set = CreateObjectSet(); set.ApplyCurrentValues(modifiedEntity); return ObjectStateManager.GetObjectStateEntry(modifiedEntity); } – kolhapuri Apr 27 '11 at 21:53
  • @kolhapuri: Tyr using the second overload of `GetObjectSateEntry` which accepts `EntityKey`. Getting entity key from POCO is little bit hareder. Check this answer: http://stackoverflow.com/questions/5273416/entity-framework-simple-generic-getbyid-but-has-differents-pk-name/5278684#5278684 I describe there how to get it for another purpose. – Ladislav Mrnka Apr 27 '11 at 22:00
0

Suppose that your domain service object is DsrvObj

 DsrvObj.EntityContainer.GetChanges() 
                      ...GetChanges().AddedEntities.Count /*also possible for modified and romoved ones*/

//These ones could be beneficial also

           DsrvObj.HasChanges
           DsrvObj.MS_EntitySets.HasChanges

will give you the changeset, But interestingly I can't see some modified boolean fields on this changeset today! Finally I realized that for instance if a DataGrid is in EditMode your changes doesnt go to changeset,After end edit it goes to changeset.

implement it ,test it ,Trust it!

Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83