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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With