0

所以我使用的是 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}

这一切似乎都是正确的,所以我不确定真正的问题在哪里。

任何人都可以提供任何帮助将不胜感激。谢谢你!

4

1 回答 1

1

我认为您展示的 EF 模型是您没有建立稳固关系的原因。您有一系列 Lines 但 Lines 没有返回 Order 的地图?

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 的地方使用 long

于 2013-10-10T11:13:12.950 回答