Example #1
0
        public void Ambient_TransactionScope_test()
        {
            using (var transactionScope = new TransactionScope())
            {
                using (var scope = DatabaseFixture.CreateTransactionScope())
                {
                    using (var db1 = scope.CreateDbContext <StoreContext>())
                        using (var db2 = scope.CreateDbContext <StoreContext>())
                        {
                            db1.Customers.Add(new Customer {
                                CustomerCode = "P1", Name = "Product 1"
                            });
                            db2.Customers.Add(new Customer {
                                CustomerCode = "P2", Name = "Product 2"
                            });
                            db1.SaveChanges();
                            db2.SaveChanges();
                        }

                    using (var db = scope.CreateDbContext <StoreContext>())
                    {
                        db.Customers.Should().HaveCount(2,
                                                        "DbContext created in the same scope must run in the same transaction");
                    }

                    scope.Commit();
                }

                transactionScope.Complete();
            }
        }