【发布时间】:2014-10-18 18:38:07
【问题描述】:
我有一个带有数据库导航属性的模型。模型结构如下
public class Parent{
public string propertyone {get; set;}
public IList<Child> children {get; set;}
}
public class Child{
public string propertyone {get; set;}
public string propertytwo {get; set;}
public IList<Seed> children {get; set;}
}
使用微风,我从数据库中加载一个“父”类型的对象,并在表格中显示“子”属性。
this.parent= manager.fetchEntityByKey('Parent', 42)
点击一个按钮,我想添加另一个孩子
<table data-bind="with: parent">
<tr data-bind="foreach: children">
<td data-bind="text: children"></td>
<td data-bind="text: children"></td>
<td><a data-bind="click: $root.remove">Remove</a></td>
</tr>
</table>
<a data-bind="click: $root.add">Add</a>
我的 add 函数不起作用看起来像这样,其中 self 代表返回的视图模型。
function add(parent){
var newchild= ko.observable(entityType.createEntity(
{
propertyone : "Test"
}));
parent.children.push(newchild);
//parent().children().push(newchild);
//self.parent.children.push(newchild);
//self.parent().children().push(newchild);
}
这以及添加函数中的注释行是我尝试过的,但这不起作用。我收到错误undefined is not a function。如何向此导航集合中添加一个表示父实体和子实体之间的一对多关系的项目?
【问题讨论】:
-
你在使用实体框架吗?如果您的元数据设置正确,您只需要
entityType.createEntity({ parentIdProperty: 'parent id value' })。这将创建实体并将其添加到父实体的集合中。 -
谢谢!!是的,我使用的是 EF,你的说法是正确的。它实际上正在添加。如果您将其发布为答案,我会接受此
标签: c# knockout.js breeze one-to-many hottowel