0
votes

So I am using an MVC4 Application Template. I have defined a very simple code first data model to illustrate my problem. I am using Breeze to supply my data and I am using Knockout to bind the data to the page.

Here is my Orders Table:

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

Here is my Lines Table:

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

I then populate my database as follows:

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');

So basically every order has 3 detail lines.

Then in my index view I do the following:

@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 -->

What I would expect to see is something like:

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

But instead what I get is:

Customer 1  
Customer 1 Line 1

Customer 2   
Customer 1 Line 2

Customer 3   
Customer 1 Line 3

So it appears as if it is linking the Order table Id field to the Line table Id field instead of the OrderId field which is the correct foreign key field.

I am not sure if this is a breeze issue, an entity framework issue or a knockout issue.

if I look at the data coming back from Orders Breeze Controller in the chrome developer tools it appears to be sending back the correct data:

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}

Which all appears correct so I am not sure where the real problem is.

Any help anybody could offer would be appreciated. Thank You!

1
I think your server side model is where the problem is.... You don't have an order navigation property back to the orders - PW Kad

1 Answers

1
votes

I think the EF models that you have shown are why you aren't getting a solid relationship. You have a collection of Lines but Lines do not have a map back to Order?

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

Here is my Lines Table class:

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; }
}

Also, you are using long's where I think you mean to use int's