//[Produces("application/xml")] public ActionResult <Invoice> GetById(int id) { try { //var invoice = _context.Invoices // .Include(i => i.Customer) // .Include(i => i.InvoiceItems) // .ThenInclude(i => i.Product) // .SingleOrDefault(i => i.InvoiceId == id); //explicit loading var invoice = _context.Invoices.SingleOrDefault(i => i.InvoiceId == id); _context.Entry(invoice).Collection(i => i.InvoiceItems).Load(); _context.Entry(invoice).Reference(i => i.Customer).Load(); foreach (var iteam in invoice.InvoiceItems) { _context.Entry(iteam).Reference(i => i.Product).Load(); } return(invoice); } catch { return(StatusCode(500, "Server error!")); } }
public ActionResult <Customer> GetById(int id) { //var customer = (from c in _context.Customers // where c.CustomerId == id // select c) // .Include(c => c.Invoices) // .ThenInclude(c => c.InvoiceItems) // .ThenInclude(c => c.Product) // .SingleOrDefault(); //explicit loading var customer = _context.Customers.SingleOrDefault(c => c.CustomerId == id); _context.Entry(customer).Collection(c => c.Invoices).Load(); foreach (Invoice inv in customer.Invoices) { _context.Entry(inv).Collection(inv => inv.InvoiceItems).Load(); foreach (InvoiceItem iteam in inv.InvoiceItems) { _context.Entry(iteam).Reference(i => i.Product).Load(); } } return(customer); }
public ActionResult <Product> GetById(int id) { var product = (from p in _context.Products where p.ProductId == id select p) .SingleOrDefault(); //explicit loading of single element _context.Entry(product).Collection(p => p.InvoiceItems).Load(); return(product); }