1

I would like to change the display of objects if they have unsaved changes. I thought I could just use ChangeTracker.State, but that doesn't change to Modified if I change a Navigation Property.

For example, a Contact object contains Navigation Properties for Phones, Addresses, and Emails. If I change the Contact's Name, the state will change to Modified but changing a Phone leaves the object's state Unchanged.

Rachel
  • 130,264
  • 66
  • 304
  • 490

2 Answers2

1

State associated with a single entity doesn't reflect state of the object graph. How do you expect it should work? It would result in setting all entities to modified once anything in the graph changes because everything is somehow related to each other.

You can subscribe ObjectStateChanging events of your change trackers and be notified about changes in the graph.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • I guess it makes sense if I think about it that way. I was hoping EF had some kind of built-in way to tell if a graph had changes to it, but it sounds like it doesn't. Thanks – Rachel Jun 06 '11 at 18:44
0

Everything is in the ChangeTracker.

You need to look at:

  • State
  • ObjectsAddedToCollection
  • ObjectsRemovedFromCollection
  • OriginalValues

You might also want to look at ExtendedProperties if you're generating your model without Foreign Key IDs and you have no repopulation policy in practice when you deserialize the STE.

David Rodrigues
  • 622
  • 5
  • 8
  • `ChangeTracker.State` does not get updated when a Navigation Property gets updated though. For example, if I update `Contact.Phones[0].PhoneNumber`, the `Contact.ChangeTracker.State` is still `Unmodified`. I was hoping there was a built-in way to get the actual object state using EF – Rachel Jun 06 '11 at 16:59
  • Nope, you need to look at several things. The idea is allways when you attach STEs back to the context, EF will do the work for you, and it will look at all those things. But in real life, you need to make decisions on whether to send things through the network or not, and to do that you need to look at those things in ChangeTracker. – David Rodrigues Jun 06 '11 at 17:32