6
我無疑在這裏丟失了一些明顯的東西。這是我創建的第一個前端應用程序。Breeze.js正確的方法刪除一對多關係中的實體
該場景如下。我有一個親子關係(一對多)。我可以創建子項,但刪除失敗。我創建了一個孩子,保存它,但是當我刪除,我得到:
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 ]
我也注意到,當刪除被髮送到服務器,孩子的「修飾」 0的parentid和實體狀態。我預計這會被「刪除」。
相關視圖模型部分:
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>
數據模型:
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; }
}