Ejemplo n.º 1
0
        /// <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();
                }
            }
        }
Ejemplo n.º 2
0
    { // TODO: 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.

        static void Main()
        {
            Order newOrder = new Order();
            newOrder.OrderDate = DateTime.Now;
            newOrder.RequiredDate = DateTime.Now + new TimeSpan(3, 0, 0, 0); //thre days later
            newOrder.ShipCountry = "Bulgaria";
            newOrder.ShipCity = "Burgas";
            int numberOfRepeatsUntilSuccess = 3;
            try
            {
                CreateOrder(newOrder, numberOfRepeatsUntilSuccess);
            }
            catch (InvalidOperationException ex) 
            {
                Console.WriteLine("{0} \n{1}", ex.Message, ex.InnerException.Message);
            }
        }
Ejemplo n.º 3
0
        private static void CreateOrder(Order newOrder, int numberOfRepeatsUntillSuccess)
        {
            bool isTransactionSucceed = false;
            using (NorthwindEntities dbContext = new NorthwindEntities())
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    for (int i = 0; i < numberOfRepeatsUntillSuccess; i++)
                    {
                        try
                        {
                            dbContext.Orders.Add(newOrder);
                            dbContext.SaveChanges();
                            transaction.Complete();
                            isTransactionSucceed = true;
                            break;
                        }
                        catch (UpdateException ex)
                        {
                            if (i == numberOfRepeatsUntillSuccess - 1)
                            {
                                throw new InvalidOperationException("Cannot complete order creation", ex);
                            }
                        }
                    }

                    if (isTransactionSucceed)
                    {
                        // Reset the context since the operation succeeded.
                        Console.WriteLine("Transaction sompited");
                    }
                    else
                    {
                        Console.WriteLine("The operation could not be completed in "
                            + numberOfRepeatsUntillSuccess + " tries.");
                    }
                }
            }
        }
 private bool FilterOrders(Order entity)
 {
     return (entity.CustomerID == this.CustomerID);
 }
 private bool FilterOrder(Order entity)
 {
     return (entity.OrderID == this.OrderID);
 }
 private void DetachOrders(Order entity)
 {
     entity.Customer = null;
 }
 private void AttachOrders(Order entity)
 {
     entity.Customer = this;
 }
Ejemplo n.º 8
0
 /// <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;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Orders EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToOrders(Order order)
 {
     base.AddObject("Orders", order);
 }
 public static void AddOrder(NorthwindEntities northwind, Order order)
 {
     northwind.Orders.Add(order);
     northwind.SaveChanges();
 }