2013-10-09 46 views
0

所以我使用的是MVC4應用程序模板。我已經定義了一個非常簡單的代碼第一個數據模型來說明我的問題。我使用Breeze提供數據,我使用Knockout將數據綁定到頁面。Breeze擴展子記錄似乎沒有使用正確的外鍵

這裏是我的訂單表:

public class Order 
{ 
    [Key] 
    public long Id { get; set; } 
    public string Customer { get; set; } 
    [ForeignKey("OrderId")] 
    public virtual ICollection<Line> Lines { get; set; } 
} 

這是我行表:

public class Line 
{ 
    [Key] 
    public long Id { get; set; } 
    public long OrderId { get; set; } 
    public string Description { get; set; } 
} 

我然後填充我的數據庫如下:

Insert into Orders(Customer) values ('Customer 1'); 
Insert into Orders(Customer) values ('Customer 2'); 
Insert into Orders(Customer) values ('Customer 3'); 
Insert into Lines (OrderId, Description) values (1,'Customer 1 Line 1'); 
Insert into Lines (OrderId, Description) values (1,'Customer 1 Line 2'); 
Insert into Lines (OrderId, Description) values (1,'Customer 1 Line 3'); 
Insert into Lines (OrderId, Description) values (2,'Customer 2 Line 1'); 
Insert into Lines (OrderId, Description) values (2,'Customer 2 Line 2'); 
Insert into Lines (OrderId, Description) values (2,'Customer 2 Line 3'); 
Insert into Lines (OrderId, Description) values (3,'Customer 3 Line 1'); 
Insert into Lines (OrderId, Description) values (3,'Customer 3 Line 2'); 
Insert into Lines (OrderId, Description) values (3,'Customer 3 Line 3'); 

所以基本上每個訂單都有3個細節線。

然後在我的索引視圖我做到以下幾點:

@section scripts { 
<script> 
    $(function() { 
     var manager = new breeze.EntityManager('breeze/breeze'); 

     var query = new breeze.EntityQuery() 
      .from("Orders").expand("Lines"); 

     manager.executeQuery(query).then(function (data) { 
      ko.applyBindings(data); 
     }).fail(function (e) { 
      alert(e); 
     }); 
    }); 
</script> 
} 
<h2>Orders</h2> 

<!-- ko foreach: results --> 
     <br /><label data-bind="text: Customer"></label>  
     <!-- ko foreach: Lines -->     
      <label data-bind="text: Description"></label>     
     <!-- /ko --> 
<!-- /ko --> 

什麼我希望看到的是一樣的東西:

Customer 1 
Customer 1 Line 1 
Customer 1 Line 2 
Customer 1 Line 3 

Customer 2 
Customer 2 Line 1 
Customer 2 Line 2 
Customer 2 Line 3 

Customer 3 
Customer 3 Line 1 
Customer 3 Line 2 
Customer 3 Line 3 

但不是我所得到的是:

Customer 1 
Customer 1 Line 1 

Customer 2 
Customer 1 Line 2 

Customer 3 
Customer 1 Line 3 

因此,看起來好像它將Order table Id字段鏈接到Line table Id字段而不是OrderId字段whi ch是正確的外鍵字段。

我不確定這是不是一件輕而易舉的事情,一個實體框架問題或一個挖空問題。

,如果我看的訂單回來微風控制器在這似乎是發回正確的數據Chrome開發人員工具中的數據:

0: {$id:1, $type:BreezeSample.Order, BreezeSample, Id:1, Customer:Customer 1,…} 
$id: "1" 
$type: "BreezeSample.Order, BreezeSample" 
Customer: "Customer 1" 
Id: 1 
Lines: [{$id:2, $type:BreezeSample.Line, BreezeSample, Id:1, OrderId:1, Description:Customer 1 Line 1},…] 
0: {$id:2, $type:BreezeSample.Line, BreezeSample, Id:1, OrderId:1, Description:Customer 1 Line 1} 
1: {$id:3, $type:BreezeSample.Line, BreezeSample, Id:2, OrderId:1, Description:Customer 1 Line 2} 
2: {$id:4, $type:BreezeSample.Line, BreezeSample, Id:3, OrderId:1, Description:Customer 1 Line 3} 
1: {$id:5, $type:BreezeSample.Order, BreezeSample, Id:2, Customer:Customer 2,…} 
$id: "5" 
$type: "BreezeSample.Order, BreezeSample" 
Customer: "Customer 2" 
Id: 2 
Lines: [{$id:6, $type:BreezeSample.Line, BreezeSample, Id:4, OrderId:2, Description:Customer 2 Line 1},…] 
0: {$id:6, $type:BreezeSample.Line, BreezeSample, Id:4, OrderId:2, Description:Customer 2 Line 1} 
1: {$id:7, $type:BreezeSample.Line, BreezeSample, Id:5, OrderId:2, Description:Customer 2 Line 2} 
2: {$id:8, $type:BreezeSample.Line, BreezeSample, Id:6, OrderId:2, Description:Customer 2 Line 3} 
2: {$id:9, $type:BreezeSample.Order, BreezeSample, Id:3, Customer:Customer 3,…} 
$id: "9" 
$type: "BreezeSample.Order, BreezeSample" 
Customer: "Customer 3" 
Id: 3 
Lines: [{$id:10, $type:BreezeSample.Line, BreezeSample, Id:7, OrderId:3, Description:Customer 3 Line 1},…] 
0: {$id:10, $type:BreezeSample.Line, BreezeSample, Id:7, OrderId:3, Description:Customer 3 Line 1} 
1: {$id:11, $type:BreezeSample.Line, BreezeSample, Id:8, OrderId:3, Description:Customer 3 Line 2} 
2: {$id:12, $type:BreezeSample.Line, BreezeSample, Id:9, OrderId:3, Description:Customer 3 Line 3} 

所有出現正確的,所以我不知道在那裏真正的問題是。

任何人可以提供幫助將不勝感激。謝謝!

+0

我覺得你的服務器端模型是哪裏出了問題是....您沒有訂單導航屬性返回到訂單 –

回答

1

我認爲你已經顯示的EF模型是爲什麼你沒有得到穩固的關係。你有一組線,但線沒有地圖返回到訂單?

public class Order 
{ 
    [Key] 
    public int Id { get; set; } 
    public string Customer { get; set; } 
    [ForeignKey("OrderId")] 
    public virtual ICollection<Line> Lines { get; set; } 
} 

這裏是我的臺詞 類:

public class Line 
{ 
    [Key] 
    public int Id { get; set; } 
    public int OrderId { get; set; } 
    public string Description { get; set; } 

    public virtual Order Order { get; set; } 
} 

此外,您使用的龍的,我覺得你的意思是使用int的

+0

您是對的。這是問題所在。我沒有意識到你需要回頭參考。我剛剛開始使用Entity Framework和Breeze,所以我非常感謝幫助。非常感謝你。對於Long vs Int公司,我使用的公司遇到了一個問題,他們超出了SQL中的Int數據類型(超過21億條記錄),不得不轉換爲BigInt(即長),這是一場惡夢因爲我已經使用BigInt的主鍵來防止這種痛苦,因爲我的桌子在將來會增長到這樣的大小。 – LarryDev