public async Task <IActionResult> SyncOrderLineItems() { var errorList = new List <string>(); try { if (_db.Connection.State == System.Data.ConnectionState.Closed) { await _db.Connection.OpenAsync(); } var query = new OrderQueries(_db); var orders = await query.GetAllOrderDetails(); foreach (var order in orders) { try { var orderId = int.Parse(order.order_id.ToString()); var productId = int.Parse(order._product_id); if (orderId >= 75746 || !_context.Product.Where(o => o.ProductId == productId).Any() || !_context.Order.Where(o => o.OrderId == orderId).Any()) { continue; } var newOrder = new OrderDetail { Amount = decimal.Parse(order._qty), ProductId = productId, SubTotal = string.IsNullOrEmpty(order._line_subtotal) ? 0 : decimal.Parse(order._line_subtotal), Total = string.IsNullOrEmpty(order._line_total) ? 0 : decimal.Parse(order._line_total), UnitPrice = string.IsNullOrEmpty(order._line_price) ? 0 : decimal.Parse(order._line_price), OrderId = orderId }; newOrder.Product = null; newOrder.DiscountAmount = 0; newOrder.TotalDiscount = 0; await _context.OrderDetail.AddAsync(newOrder); await _context.SaveChangesAsync(); } catch (Exception ex) { errorList.Add("order line items:" + ex.ToString()); } } _db.Connection.Close(); } catch (Exception ex) { errorList.Add("order line items:" + ex.ToString()); } return(Ok(errorList)); }