2013-08-18 34 views
0

我相信我做错了什么,但我无法弄清楚。我正在使用Breezejs Todo + Knockout示例来重现我的问题。我有以下数据模型:Breeze.js实体框架多个一对一的相同类型(父 - >儿童childOne,儿童childTwo)

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 property id 
Parent property otherProperty 
childOne undefined 
childTwo undefined 

回答

0

布洛克, 你不能有相同类型的多个一到一个关联。

EF不支持这种情况,原因是在一对一关系中,EF要求依赖关系的主键也是外键。此外,EF无法“知道”Child实体中关联的另一端(即,Child实体中的Parent导航的InverseProperty是什么? - ChildOne或ChildTwo?)

In a一到一个关联中,你还必须定义主/从属:

modelBuilder.Entity<Parent>() 
     .HasRequired(t => t.ChildOne) 
     .WithRequiredPrincipal(t => t.Parent); 

你可能要检查http://msdn.microsoft.com/en-US/data/jj591620关于配置关系的细节。

而不是2个一对一的关系,你可能想要一个一对多的关联,并在代码中处理它,所以它只有2个子元素。您可能还需要Child实体中的其他属性来确定该孩子是“ChildOne”还是“ChildTwo”。

+0

好的,这是我想的,但我只是想确定,因为这是一个痛苦的解决。 –