2013-08-18 57 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

好的,這是我想的,但我只是想確定,因爲這是一個痛苦的解決。 –