Ejemplo n.º 1
0
        public void TransactionScopeCommit()
        {
            Customer customer = new Customer();

            customer.CompanyName = "company";
            customer.CustomerID = "CUSTO";

            using (TransactionScope tran = new TransactionScope())
            {
                context.Customers.AddObject(customer);
                context.SaveChanges();

                tran.Complete();
            }

            bool customerWasAdded = context.Customers.FirstOrDefault(c => c.CustomerID == "CUSTO") != null;

            Assert.IsTrue(customerWasAdded);
        }
Ejemplo n.º 2
0
        public void TransactionScopeRollback()
        {
            Customer customer = new Customer();

            customer.CompanyName = "company";
            customer.CustomerID = "CUSTO";

            using (TransactionScope tran = new TransactionScope())
            {
                this.context.Customers.AddObject(customer);
                this.context.SaveChanges();

                bool customerWasAdded = context.Customers.FirstOrDefault(c => c.CustomerID == "CUSTO") != null;
                Assert.IsTrue(customerWasAdded);

                // Omit 'tran.Complete()' to achieve rollback
            }

            bool customerWasNotAdded = context.Customers.FirstOrDefault(c => c.CustomerID == "CUSTO") == null;
            Assert.IsTrue(customerWasNotAdded);
        }