/// <summary>
        /// EF works with transactions by default - calling SaveChanges() starts a transaction by default.
        /// However we can use transactions to unify many calling of SaveChanges() as one and if one fails - all fail.
        /// </summary>
        public static void Main()
        {
            using (var db = new NorthwindEntities())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    var order = new Order()
                    {
                        CustomerID = "WELLI",
                        EmployeeID = 4
                    };

                    db.Orders.Add(order);

                    var orderDetails1 = new Order_Detail()
                    {
                        OrderID = order.OrderID,
                        ProductID = 5,
                        UnitPrice = 12.34m,
                        Quantity = 100,
                        Discount = 0.2f
                    };

                    var orderDetails2 = new Order_Detail()
                    {
                        OrderID = order.OrderID,
                        ProductID = 3,
                        UnitPrice = 12.34m,
                        Quantity = 100,
                        Discount = 0.2f
                    };

                    var orderDetails3 = new Order_Detail()
                    {
                        OrderID = order.OrderID,
                        ProductID = 4,
                        UnitPrice = 12.34m,
                        Quantity = 100,
                        Discount = 0.2f
                    };

                    db.Order_Details.AddRange(new[] { orderDetails1, orderDetails2, orderDetails3 });

                    try
                    {
                        db.SaveChanges();
                    }
                    catch (System.Exception ex)
                    {
                        System.Console.WriteLine(ex);
                        transaction.Rollback();
                    }

                    transaction.Commit();
                }
            }
        }
 private bool FilterOrder_Details(Order_Detail entity)
 {
     return (entity.ProductID == this.ProductID);
 }
 private void AttachOrder_Details(Order_Detail entity)
 {
     entity.Product = this;
 }
 private void DetachOrder_Details(Order_Detail entity)
 {
     entity.Product = null;
 }
 private bool FilterOrder_Details(Order_Detail entity)
 {
     return (entity.OrderID == this.OrderID);
 }
 private void DetachOrder_Details(Order_Detail entity)
 {
     entity.Order = null;
 }
 private void AttachOrder_Details(Order_Detail entity)
 {
     entity.Order = this;
 }
Exemple #8
0
 /// <summary>
 /// Create a new Order_Detail object.
 /// </summary>
 /// <param name="orderID">Initial value of the OrderID property.</param>
 /// <param name="productID">Initial value of the ProductID property.</param>
 /// <param name="unitPrice">Initial value of the UnitPrice property.</param>
 /// <param name="quantity">Initial value of the Quantity property.</param>
 /// <param name="discount">Initial value of the Discount property.</param>
 public static Order_Detail CreateOrder_Detail(global::System.Int32 orderID, global::System.Int32 productID, global::System.Decimal unitPrice, global::System.Int16 quantity, global::System.Single discount)
 {
     Order_Detail order_Detail = new Order_Detail();
     order_Detail.OrderID = orderID;
     order_Detail.ProductID = productID;
     order_Detail.UnitPrice = unitPrice;
     order_Detail.Quantity = quantity;
     order_Detail.Discount = discount;
     return order_Detail;
 }
Exemple #9
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Order_Details EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToOrder_Details(Order_Detail order_Detail)
 {
     base.AddObject("Order_Details", order_Detail);
 }