gpt4 book ai didi

javascript - 在一对多关系中删除实体的正确方法

转载 作者:数据小太阳 更新时间:2023-10-29 04:38:50 25 4
gpt4 key购买 nike

我有一个父子关系(一对多)。我能够创建 child ,但删除失败。我创建了一个 child ,保存它,但是当我删除时,我得到:

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 ]

我确实注意到,当删除被发送到服务器时,子项的 parentid 为 0,实体状态为“已修改”。我希望这会被“删除”。

相关 View 模型部分:

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; }
}

最佳答案

确实我没有正确删除实体。我应该:

child.entityAspect.setDeleted();

我以为我已经阅读了 change tracking正确记录但实际上我没有。

关于javascript - 在一对多关系中删除实体的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18550539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com