public async Task <IActionResult> DeleteOrder([FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Retrieve order with details var order = await _context.Orders .Include(m => m.OrderDetails) .SingleOrDefaultAsync(m => m.OrderId == id); if (order == null) { return(NotFound()); } // Set tracking state to deleted order.TrackingState = TrackingState.Deleted; // Detach object graph _context.DetachEntities(order); // Apply changes to context _context.ApplyChanges(order); // Persist changes await _context.SaveChangesAsync(); return(Ok()); }