public static void AddOrderDetails(List<Product> products, Order o) { try { using (TransactionScope ts = new TransactionScope()) { foreach (Product p in products) { Order_Detail od = new Order_Detail(); od.Order = o; od.Product = p; od.Quantity = 1; } db.SaveChanges(); ts.Complete(); } } catch (TransactionAbortedException ex) { Console.WriteLine("TransactionAbortedException Message: {0}", ex.Message); } catch (ApplicationException ex) { Console.WriteLine("ApplicationException Message: {0}", ex.Message); } }
static void Main(string[] args) { //Create a method that places a new order in the Northwind database. // The order should contain several order items. // Use transaction to ensure the data consistency. Product p1 = new Product(); p1.ProductName = "Tesla"; p1.UnitPrice = 20.20M; DAO.db.Products.AddObject(p1); Product p2 = new Product(); p2.ProductName = "Kashon"; p2.UnitPrice = 14.20M; DAO.db.Products.AddObject(p2); List<Product> products = new List<Product>(); products.Add(p1); products.Add(p2); Order order = new Order(); order.ShipCountry = "Bulgaria"; order.CustomerID = "ALFKI"; DAO.db.Orders.AddObject(order); DAO.db.SaveChanges(); DAO.AddOrderDetails(products, order); }
/// <summary> /// Deprecated Method for adding a new object to the Orders EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToOrders(Order order) { base.AddObject("Orders", order); }
/// <summary> /// Create a new Order object. /// </summary> /// <param name="orderID">Initial value of the OrderID property.</param> public static Order CreateOrder(global::System.Int32 orderID) { Order order = new Order(); order.OrderID = orderID; return order; }