public void UnitOfWork_nested_commit_with_seperate_transaction_commits_when_wrapping_scope_rollsback() { // Generate Test Data this.Logger.LogInformation("Generating Test Data for: " + MethodBase.GetCurrentMethod(), null); _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); // Setup required services var scopeFactory = this.ServiceProvider.GetService <IUnitOfWorkScopeFactory>(); Customer customer = null; testData.Batch(x => customer = x.CreateCustomerStub()); var order = new Order { OrderDate = DateTime.Now, ShipDate = DateTime.Now }; this.Logger.LogInformation("Starting initial UnitOfWorkScope from " + MethodBase.GetCurrentMethod(), null); using (var scope = scopeFactory.Create(TransactionMode.Default)) { var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; this.Logger.LogInformation("Adding New Customer from first UnitOfWorkScope ", customer); repo.Add(customer); this.Logger.LogInformation("Starting new UnitOfWorkScope from " + MethodBase.GetCurrentMethod(), null); using (var scope2 = scopeFactory.Create(TransactionMode.New)) { var repo2 = this.ServiceProvider.GetService <IEagerFetchingRepository <Order> >(); repo2.DataStoreName = "TestDbContext"; this.Logger.LogInformation("Adding New Order from first UnitOfWorkScope ", order); repo2.Add(order); this.Logger.LogInformation("Attempting to Commit second(new) UnitOfWorkScope ", scope2); scope2.Commit(); } } //Rollback this.Logger.LogInformation("Attempting to Rollback back initial UnitofWorkScope ", null); Customer savedCustomer = null; Order savedOrder = null; savedCustomer = testDataActions.GetCustomerById(customer.CustomerId); savedOrder = testDataActions.GetOrderById(order.OrderId); Assert.IsNull(savedCustomer); Assert.IsNotNull(savedOrder); Assert.IsTrue(customer.CustomerId == 0); // First transaction does not commit Assert.AreEqual(order.OrderId, savedOrder.OrderId); // Second transaction does commit because it is marked "new" }
public void UnitOfWork_nested_commit_works() { // Generate Test Data _context = new TestDbContext(this.Configuration); var testData = new EFTestData(_context); var testDataActions = new EFTestDataActions(testData); var customer = testDataActions.CreateCustomerStub(); var order = testDataActions.CreateOrderStub(); // Setup required services var scopeFactory = this.ServiceProvider.GetService <IUnitOfWorkScopeFactory>(); using (var scope = scopeFactory.Create(TransactionMode.Default)) { var repo = this.ServiceProvider.GetService <IEagerFetchingRepository <Customer> >(); repo.DataStoreName = "TestDbContext"; repo.Add(customer); //scope.Commit(); using (var scope2 = scopeFactory.Create(TransactionMode.Default)) { var repo2 = this.ServiceProvider.GetService <IEagerFetchingRepository <Order> >(); repo2.DataStoreName = "TestDbContext"; repo2.Add(order); scope2.Commit(); } scope.Commit(); } Customer savedCustomer = null; Order savedOrder = null; savedCustomer = testDataActions.GetCustomerById(customer.CustomerId); savedOrder = testDataActions.GetOrderById(order.OrderId); Assert.IsNotNull(savedCustomer); Assert.AreEqual(customer.CustomerId, savedCustomer.CustomerId); Assert.IsNotNull(savedOrder); Assert.AreEqual(order.OrderId, savedOrder.OrderId); }