public void SaveOrderItem(OrderItem orderItem) { if (orderItem.OrderItemID == 0) { orderItem.CreatedAt = DateTime.Now; orderItem.UpdatedAt = DateTime.Now; context.OrderItems.Add(orderItem); } else { OrderItem dbEntry = context.OrderItems.Find(orderItem.OrderItemID); if (dbEntry != null) { dbEntry.Name = orderItem.Name; dbEntry.Description = orderItem.Description; dbEntry.Price = orderItem.Price; dbEntry.Category = orderItem.Category; dbEntry.ImageData = orderItem.ImageData; dbEntry.ImageMimeType = orderItem.ImageMimeType; dbEntry.Special = orderItem.Special; dbEntry.Seller = orderItem.Seller; dbEntry.Buyer = orderItem.Buyer; dbEntry.Quantity = orderItem.Quantity; dbEntry.UpdatedAt = DateTime.Now; } } try { context.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } catch (Exception e) { int i = 0; Console.WriteLine("In Main catch block. Caught: {0}", e.Message); Console.WriteLine("Inner Exception is {0}", e.InnerException); } }
public int StoreOrderAndOrderItems(Cart cart) { Order order = new Order(); //order.OrderID order.OrderDate = DateTime.Now; order.ShipDate = DateTime.Now; order.TotalOrder = cart.ComputeTotalValue(); order.ProductCount = cart.ComputeNumItems(); order.FirstName = string.Empty; order.LastName = string.Empty; order.Company = string.Empty; order.Email = string.Empty; order.StreetLine1 = string.Empty; order.StreetLine2 = string.Empty; order.StreetLine3 = string.Empty; order.City = string.Empty; order.PostalCode = string.Empty; order.County = string.Empty; order.Country = string.Empty; order.PaymentConfirmation = string.Empty; order.CreatedAt = DateTime.Now; order.UpdatedAt = DateTime.Now; order.BillToAddressID = 1; order.ShipToAddressID = 1; order.CustomerID = 1; orderRepository.SaveOrder(order); foreach (var line in cart.Lines) { OrderItem orderItem = new OrderItem(); Product product = repository.Products .FirstOrDefault(p => p.ProductID == line.Product.ProductID); //orderItem.OrderItemID orderItem.Name = line.Product.Name; orderItem.Description= line.Product.Description; orderItem.Price = line.Product.Price; orderItem.Category = line.Product.Category; orderItem.Special = line.Product.Special; orderItem.ImageData = line.Product.ImageData; orderItem.ImageMimeType = line.Product.ImageMimeType; orderItem.Seller = line.Product.Seller; orderItem.Buyer = line.Product.Buyer; orderItem.Quantity = line.Quantity; orderItem.OrderID = order.OrderID; orderItem.ProductID = product.ProductID; orderItem.CategoryID = product.CategoryID; orderItemRepository.SaveOrderItem(orderItem); } return order.OrderID; }