10

I have a database model like this:

public class Customer
{
    public int CustomerId{ get; set; }

    public int OrderId { get; set; }
    public ICollection<Order> Orders { get; set; }
}

public class Order
{
    public int OrderId { get; set; }
    public int Amount { get; set; }

    public int ProductId { get; set; }
    public Product Product { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
}

So we have a customer which can do an order. This order includes a product and this includes a name. I'm now trying to return the complete model with a linq statement like the following:

_db.Customer.Include(c => c.Orders).ThenInclude(o => o.Product).SingleOrDefaultAsync();

But ThenInclude(o => o.Product) doesn't function because Orders is an ICollection. Greetings from Germany and thanks in advance for any help.

steamrolla
  • 2,373
  • 1
  • 29
  • 39
Marvin Püthe
  • 101
  • 1
  • 1
  • 4

2 Answers2

8

To load related entities in EF 6 you should use Select method as I show below:

_db.Customer.Include(c => c.Orders.Select(o=>o.Product)).SingleOrDefaultAsync();

ThenInclude method was added in EF Core, which is the last version of EF.

ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • The **old** way? OP is either on EF6 and this is the **only** way, or is on EF Core and this is not supported at all :) – Ivan Stoev Mar 10 '17 at 18:54
  • Yes, but I thought he was using EF Core, that's why I called that way – ocuenca Mar 10 '17 at 18:55
  • Then you've got a problem because EF Core does not support the "old way". – Ivan Stoev Mar 10 '17 at 18:56
  • Are you sure? To be honest I haven't tested recently but in the last beta version was supported – ocuenca Mar 10 '17 at 18:57
  • Yes, I am. Many things have changed since beta versions (although they better be called alpha, and current - beta:), event between the official 1.0 and 1.1 (latest). – Ivan Stoev Mar 10 '17 at 18:59
  • ok, I will update my answer then, thanks for the info @IvanStoev – ocuenca Mar 10 '17 at 19:00
  • See http://stackoverflow.com/questions/41129970/can-not-load-related-data-with-include-or-theninclude-or-select-many-with-one-qu/41130631#41130631. Take care. – Ivan Stoev Mar 10 '17 at 19:01
  • Yes, I tried your answer but got the following error: System.InvalidOperationException: The property expression 'c => {from Order o in [c].Orders select [o].Product}' is not valid. – Marvin Püthe Mar 10 '17 at 19:27
1

May be wrong here but wouldn;t you need to change:

public class Order
{
    public int OrderId { get; set; }
    public int Amount { get; set; }

    public int ProductId { get; set; }
    public Product Product { get; set; }
}

to

public class Order
{
    [Key, ForeignKey("Product")]
    public int OrderId { get; set; }
    public int Amount { get; set; }

    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}

in order for this to behave the way you are expecting.

Travis Acton
  • 4,292
  • 2
  • 18
  • 30