2013-08-31 51 views
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; } 
} 

回答

7

事实上,我并没有正确删除实体。我应该:

child.entityAspect.setDeleted(); 

我以为我已经正确读取change tracking文档,但其实我没有。

相关问题