Parent/child relationship when editing parent as detail view

Feb 28, 2012 at 10:02 AM

I have entities Profile and Contact. What I would like is to have a ProfileDetailView where you can edit the main entity (Profile) info, and on the 
same view to have a gridview of Contacts with add/edit/delete functionality.

On ProfileListView, at editProfileButton_Click, I have the classic 

ProfileDetailViewModel detailModel = new ProfileDetailViewModel(model.SelectedProfile);
which loads detailModel containing all related Contacts. However the next line (as provided by templates),
clears all the Contacts from the detailModel.
Contacts datagrid, binded to Model.Contacts on the ProfileDetailView is thus loaded empty. However if I click cancel, for a brief moment before the 
window closes, Contacts appear in datagrid. I believe that is from after EndEdit/CancelEdit call. If I remove those, data is shown correctly.
Then in ProfileDetailView.xaml.cs at btnAddContact_Click in item.Closed callback I do

is null. This works ok.
This all leaves me a bit confused as I've been working on child CRUD functionality for a few days now and still haven't come up with anything usefull. 
Am I doing it wrong? Is there a sample? I've read which deals with a similar problem, but 
unfortunately haven't found it helpful.

Finally getting add/edit to work, I still have an issue with delete. Problem lies in not deleting a Contact from the database, but changing the model 
so that Contact.SubjectID foreign key is set to NULL, which is not allowed, causing Save to fail due to database constraints.

So ... how does one go about deleting child entities of an object shown in detail view?

Thanks and keep up the good work!

Feb 29, 2012 at 3:02 AM
Are you using the RIA Services template? If so, change tracking is built in, and the client-side entities implement IEditableObject (or some such interface). So you would use the technique mentioned in the discussion thread you referred to. Give me some time to come up with a sample.

Feb 29, 2012 at 7:26 AM

Hi Tony,

Thanks for a quick response. I am using the RIA Services template. I am also using the SaveChanges function provided with ProductListServiceAgent example. However I had to change it from

if (domainContext.Subjects.HasChanges)


if (domainContext.Subjects.HasChanges || domainContext.Contacts.HasChanges)

(check for changes in profiles and contacts, because subjects appear unchanged) but then the save fails because Contact is not removed but only "unlinked" -- foreign key SubjectID of a Contact is set to 0 which database will not allow.

Feb 29, 2012 at 11:43 AM
The way you add items on the domain context is using the Add method. It's not enough to just add it to the object graph because it has to me marked as "added".