I have an entity with a bunch of child entities as properties. Users are presented with a list of entities, can select one to modify it, then Save or Cancel the changes. Saving and Cancelling is not required to switch between entities, so its possible that more than one object is EntityState.Modified
at once.
The Save button correctly works to save both the selected entity and all child entities, however the Cancel button is only reverting the changes made to the parent entity, without reverting changes to the child entities.
Is there an easy way to revert all changes within the graph for that object, without looping through every navigation property?
I am using the following code to revert the changes:
context.Connection.Open();
context.Refresh(RefreshMode.StoreWins, MyEntity);
context.Connection.Close();
A simplified sample of my entity class structure looks like this:
class MyEntity
{
string Name;
IList<Address> Addresses;
IList<Contact> Contacts;
IList<Note> Notes;
}
class Contact
{
string Name;
string Title;
IList<EmailAddress> EmailAddresses;
IList<Phone> PhoneNumbers;
}
I did see this question, however that solution would revert changes to all unsaved entities, and not just the selected entity.
Context.Refresh(RefreshMode.StoreWins, _
Context.ObjectStateManager.GetObjectStateEntries());