所以我使用的是MVC4应用程序模板。我已经定义了一个非常简单的代码第一个数据模型来说明我的问题。我正在使用Breeze提供我的数据,我正在使用Knockout将数据绑定到页面。
这是我的订单表:
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表Id字段链接到Line表Id字段而不是OrderId字段,这是正确的外键字段。
我不确定这是一个轻微的问题,实体框架问题还是淘汰赛问题。
如果我查看Chrome开发人员工具中从Orders Breeze Controller返回的数据,它似乎正在发回正确的数据:
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; }
}
另外,你正在使用long我认为你的意思是使用int的