I am undoubtedly missing something obvious here. This is the first interface app I created.
The scenario is as follows. I have a parenting relationship with children (one for many). I can create children, but the removal failed. I create a child, save it, but when I delete, I get:
A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK_dbo.Children_dbo.Parents_ParentId ]
I noticed that when the deletion is sent to the server, the child has a parent code of 0 and the state of the entity is βchangedβ. I expect this to be "deleted."
The corresponding kind of parts of the model:
function queryFailed(error) { console.log('Query failed: ' + error.message); } function save() { dataservice.saveChanges().fail(queryFailed); } function deleteChild(child) { parent().children.remove(child); } function addChild() { dataservice.createChild(parent); }
HTML:
<section data-bind="with: parent"> <div data-bind="foreach: children"> <div> <select name="propertyOne" data-bind="options: propertyOneOptions, value: propertyOne, optionsText: 'description', optionsCaption: 'Select a Property'"> </select> <button data-bind="click: $root.deleteChild">Delete Child</button> </div> </div> <button data-bind="click: $root.addChild">Add Child</button> </section> <button data-bind="click: save">Save</button>
Data Model:
public class Parent { public Parent() { Children = new List<Child>(); } [Key] public int Id { get; set; } public String OtherProperty { get; set; } public IList<Child> Children { get; set; } } public class Child { [Key] public int Id { get; set; } public int ParentId { get; set; } [ForeignKey("ParentId")] public Parent Parent { get; set; } }
source share