Hi,
I have a Visual Studio 2010 generated set of POCO classes (just out of the box POCO templates generated from DB Schema). For a given use case I let the user load an entity (a CRM contact) and act on it - add Phone Numbers (which in itself is a separate entity related by foreign key) and address (also a separate entity) etc. In between postbacks I store the modified entities in ViewState (I don't want to save the changes to the database right away). The problem arises when the user hits the Save button. The main CRM Contact will be saved fine (any changes are detected and saved) but none of the related properties get saved - no matter if it's a new addition or modified EF just ignores it.
How do I force Entity Framework to detect that I have changes in my related properties? I'm using this to save my main contact:
//contact is an instance of CRMContact retrieved from ViewState
if (contact.Id == 0) {
CRMEntities.CRMContacts.AddObject(contact);
} else {
CRMContact orig = CRMEntities.CRMContacts.Where(c => c.Id == contact.Id).FirstOrDefault();
CRMEntities.CRMContacts.ApplyCurrentValues(contact);
}
CRMEntities.SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave);
This works fine for the contact entity but not for my related ones. What do I need to add for phone numbers and emails to be added and/or updated?
Note that I don't want to use proxy-based change tracking.
Thanks