I have a simple parent-child situation where the parent can have multiple children. The user can update the list of children at will by selecting or deselecting them from a listbox. I try updating the child list using code like below but I get a SqlException:
Violation of PRIMARY KEY constraint 'PK_Child_1'. Cannot insert duplicate key in object 'dbo.Child'.
It appears that LINQ is inserting the new children before deleting the existing ones. I'm sure there is a simple pattern for handling this but I'm stumped.
context.DeleteAllOnSubmit(parent.Children);
foreach (string childname in listBox1.SelectedItems) {
parent.Children.Add(new Child(parentkey, childname));
}
context.SubmitChanges();
Each parent has a unique key (a GUID) and all parent and child columns are non-nullable. The child table is a simple two-column table with the parent key and a varchar value, with a compound primary key consisting of both columns.
Thanks