1

New to EF Core...

I have the following EF Core class setup, but I am running into a problem loading the data into a view. When all data is populated for each table everything loads as is, however if for example I have an order and order line that has no orders received only the order loads. The orderline will not load.

Classes:

public class Order {
    public Guid OrderID { get; set;}
    public ICollection<OrderLine> OrderLines { get; set; }
}

public class OrderLine {
    public Guid OrderLineId { get; set; }
    public int OrderQty { get; set; }
    public Order Order { get; set; }
    public Guid OrderID { get; set; }
    public ICollection<OrderReceived> OrdersReceived { get; set; }
}

public class OrderReceived {
    public Guid OrderReceivedID { get; set; }
    public int QuantityReceived { get; set; }
    public OrderLine OrderLine { get; set; }
    public Guid OrderLineId { get; set; }

}

My controller looks like this:

public async Task<IActionResult> Details(Guid? id) {

    if (id == null) {
        return NotFound();
    }

    var orderDetail = await _context.Orders
        .Include(ol => ol.OrderLines)
            .ThenInclude(rec => rec.OrdersReceived)
        .SingleOrDefaultAsync(o => o.OrderId = id);

}

Ex: Order A has 1 order line and 2 OrdersReceived. This loads perfectly.

Ex: Order B has 1 order line and no orders received. Only the Order detail loads everything below that does not load (orderline or ordersreceived). I guess I'm looking for something more like a left join for the ordersreceived. I'm just not that familiar with EF Core.

Hopefully I explained this correcly. Any help is greatly appreciated. I did find this post with a similar question: ef core linq filtered child entities. Is best practice to implement a viewmodel for this type of situation?

Thank you, E

eVeezy
  • 41
  • 2
  • Check https://stackoverflow.com/questions/38813010/how-to-build-several-left-join-query-in-entity-framework-core - you might have to do some additional setup of your db context to teach ef about the relationships – Caius Jard Nov 23 '17 at 16:21

0 Answers0