Breeze.js实体框架多个一对一的相同类型(Parent->Child-childOne,Child-chil
Breeze.js Entity Framework multiple one to one of the same type (Parent -> Child childOne, Child childTwo)
我确信我做错了什么,但我无法弄清楚。我使用Breezejs Todo+Knockout示例来重现我的问题。我有以下数据模型:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Todo.Models
{
public class Parent
{
public Parent()
{
}
[Key]
public int Id { get; set; }
[Required]
public string OtherProperty { get; set; }
public Child ChildOne { get; set; }
public Child ChildTwo { get; set; }
}
public class Child
{
[Key]
public int Id { get; set; }
public int ParentId { get; set; }
[ForeignKey("ParentId")]
public Parent Parent { get; set; }
}
}
在应用程序中,我执行以下操作:
breeze.NamingConvention.camelCase.setAsDefault();
var manager = new breeze.EntityManager(serviceName);
manager.fetchMetadata().then(function () {
var parentType = manager.metadataStore.getEntityType('Parent');
ko.utils.arrayForEach(parentType.getPropertyNames(), function (property) {
console.log('Parent property ' + property);
});
var parent = manager.createEntity('Parent');
console.log('childOne ' + parent.childOne);
console.log('childTwo ' + parent.childTwo);
});
问题是childOne和childTwo没有定义为Parent的属性我的数据模型有问题吗日志消息为:
Parent property id
Parent property otherProperty
childOne undefined
childTwo undefined
Brock,同一类型不能有多个一对一关联。
EF不支持这种情况,原因是在一对一关系中,EF要求依赖项的主键也是外键。此外,EF无法"知道"子实体中关联的另一端(即子实体中父导航的InverseProperty是什么?-ChildOne还是ChildTwo?)
在一对一的关联中,您还必须定义主体/从属:
modelBuilder.Entity<Parent>()
.HasRequired(t => t.ChildOne)
.WithRequiredPrincipal(t => t.Parent);
你可能想检查一下http://msdn.microsoft.com/en-US/data/jj591620有关配置关系的详细信息。
您可能希望有一个一对多的关联,而不是2个一对一的关系,并在代码中处理它,因此它只有2个子元素。您可能还希望在Child实体中添加一个附加属性,以确定该子项是"ChildOne"还是"ChildTwo"。
相关文章:
- 在<页眉>标签
- 如何更改<svg>标记为<img>用js标记
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- Ajax文件加载和<输入>文件加载
- 为什么要使用0>javascript中的0
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 链接所有<a>Meteor
- 在<输入类型=“;文件“/>
- Div根据<选择>菜单
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- 如何首先设置样式<td>表中包含在窗体中的元素
- 通过点击<李>在jQuery中
- 正在检测导航到<a name=“;最新主题”></a>
- 将所选类别循环到ul>李用加载更多按钮
- 如何更改<选择>使用angularJS从控制器获得的值
- 使用下拉列表筛选列表(ul>li)
- 这个问号在Flow中意味着什么:“;?()=>“无效”;
- Breeze.js实体框架多个一对一的相同类型(Parent->Child-childOne,Child-chil
- jQuery$('parent>*')选择器&带有点击代理的iOS
- 为什么jQuery选择器("parent>child")会跳过子项