コード例 #1
0
        public void rollback_transaction_in_multiple_different_contexts()
        {
            PrepareUser(userId);

            var initialCount = GetUserOrdersCount(userId);

            using (var tran = new TransactionScope())
            {
                using (var db = new MyContext())
                {
                    IncreateUserOrdersCount(db);
                }

                using (var db = new OtherContext())
                {
                    AddInvoice(db);
                }
            }

            using (var db = new MyContext())
            {
                var user = db.Users.Find(userId);

                Assert.AreEqual(user.OrdersCount, initialCount);
                Assert.AreEqual(user.Orders.Count(), initialCount);
            }
            using (var db = new OtherContext())
            {
                var invoicesCount = db.Invoices.Count(i => i.UserId == userId);
                Assert.AreEqual(0, invoicesCount);
            }
        }
コード例 #2
0
 private void AddInvoice(OtherContext db)
 {
     db.Invoices.Add(new Invoice()
     {
         UserId = userId
     });
     db.SaveChanges();
 }
コード例 #3
0
        public void rollback_transaction_in_multiple_different_contexts_perf()
        {
            PrepareUser(userId);

            var initialCount = GetUserOrdersCount(userId);

            perfMeter.MeasurePerf(() =>
            {
                using (var tran = new TransactionScope())
                {
                    using (var db = new MyContext())
                    {
                        IncreateUserOrdersCount(db);
                    }

                    using (var db = new OtherContext())
                    {
                        AddInvoice(db);
                    }
                }
            });
        }