public void Can_attach_modified_entity()
        {
            var customer = new Customer
            {
                FirstName = "John",
                LastName = "Doe"
            };

            var context = (OrderEntities) OrdersContextProvider();
            context.AddToCustomers(customer);
            #if EF_1_0
            context.SaveChanges(true);
            #else
            context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
            #endif
            context.Detach(customer);
            context.Dispose();

            using (var scope = new UnitOfWorkScope())
            {
                customer.LastName = "Changed";
                var repository = new EFRepository<Customer>();
                repository.Attach(customer);
                scope.Commit();
            }

            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                testData.Batch(x => savedCustomer = x.GetCustomerById(customer.CustomerID));
                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedCustomer.LastName, Is.EqualTo("Changed"));
            }
        }
Beispiel #2
0
        public void Can_eager_fetch_using_for()
        {
            Locator.Stub(x => x.GetAllInstances <IFetchingStrategy <Customer, EFRepositoryEagerFetchingTests> >())
            .Return(new[] { new FetchingStrategy() });

            var      testData      = new EFTestData(Context);
            Customer customer      = null;
            Customer savedCustomer = null;

            testData.Batch(x =>
            {
                customer  = x.CreateCustomer();
                var order = x.CreateOrderForCustomer(customer);
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
            });

            using (var scope = new UnitOfWorkScope())
            {
                savedCustomer = new EFRepository <Customer>()
                                .For <EFRepositoryEagerFetchingTests>()
                                .Where(x => x.CustomerID == customer.CustomerID)
                                .SingleOrDefault();
                scope.Commit();
            }

            Assert.NotNull(savedCustomer);
            Assert.NotNull(savedCustomer.Orders);
            savedCustomer.Orders.ForEach(order =>
            {
                Assert.NotNull(order.OrderItems);
                order.OrderItems.ForEach(orderItem => Assert.NotNull(orderItem.Product));
            });
        }
Beispiel #3
0
        public void Can_eager_fetch_many()
        {
            var testData = new EFTestData(Context);

            Customer customer      = null;
            Customer savedCustomer = null;

            testData.Batch(x =>
            {
                customer  = x.CreateCustomer();
                var order = x.CreateOrderForCustomer(customer);
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
            });

            using (var scope = new UnitOfWorkScope())
            {
                savedCustomer = new EFRepository <Customer>()
                                .FetchMany(x => x.Orders)
                                .ThenFetchMany(x => x.OrderItems)
                                .ThenFetch(x => x.Product)
                                .Where(x => x.CustomerID == customer.CustomerID)
                                .SingleOrDefault();
                scope.Commit();
            }

            Assert.NotNull(savedCustomer);
            Assert.NotNull(savedCustomer.Orders);
            savedCustomer.Orders.ForEach(order =>
            {
                Assert.NotNull(order.OrderItems);
                order.OrderItems.ForEach(orderItem => Assert.NotNull(orderItem.Product));
            });
        }
        public void can_commit_multiple_db_operations()
        {
            var customer = new Customer { FirstName = "John", LastName = "Doe" };
            var salesPerson = new SalesPerson { FirstName = "Jane", LastName = "Doe", SalesQuota = 2000 };

            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer>().Add(customer);
                new EFRepository<SalesPerson>().Add(salesPerson);
                scope.Commit();
            }

            using (var ordersTestData = new EFTestData(OrdersContextProvider()))
            using (var hrTestData = new EFTestData(HRContextProvider()))
            {
                Customer savedCustomer = null;
                SalesPerson savedSalesPerson = null;
                ordersTestData.Batch(action => savedCustomer = action.GetCustomerById(customer.CustomerID));
                hrTestData.Batch(action => savedSalesPerson = action.GetSalesPersonById(salesPerson.Id));

                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedSalesPerson, Is.Not.Null);
                Assert.That(savedCustomer.CustomerID, Is.EqualTo(customer.CustomerID));
                Assert.That(savedSalesPerson.Id, Is.EqualTo(salesPerson.Id));
            }
        }
        public void Can_Update()
        {
            _context = new TestDbContext(this.Configuration);
            var testData        = new EFTestData(_context);
            var testDataActions = new EFTestDataActions(testData);
            var customer        = testDataActions.CreateCustomer();

            var service = this.ServiceProvider.GetService <ITestAppService>();
            var mapper  = this.ServiceProvider.GetService <IMapper>();

            var customerDto = mapper.Map <CustomerDto>(customer);
            var firstName   = new Faker().Name.FirstName();

            customerDto.FirstName = firstName;
            var result = service.Update(customerDto);

            _context.Dispose(); // Refresh the cache
            _context = new TestDbContext(this.Configuration);
            testData = new EFTestData(_context);

            Customer savedCustomer = null;

            testData.Batch(action => savedCustomer = action.GetCustomerById(customer.CustomerId));

            Assert.IsNotNull(savedCustomer);
            Assert.AreEqual(savedCustomer.FirstName, firstName);
        }
        public void Can_eager_fetch_many()
        {
            var testData = new EFTestData(Context);

            Customer customer = null;
            Customer savedCustomer = null;
            testData.Batch(x =>
            {
                customer = x.CreateCustomer();
                var order = x.CreateOrderForCustomer(customer);
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
                order.OrderItems.Add(x.CreateOrderItem(item => item.Order = order));
            });

            using (var scope = new UnitOfWorkScope())
            {
                savedCustomer = new EFRepository<Customer,int>()
                    .FetchMany(x => x.Orders)
                    .ThenFetchMany(x => x.OrderItems)
                    .ThenFetch(x => x.Product).Query
                    .Where(x => x.CustomerID == customer.CustomerID)
                    .SingleOrDefault();
                scope.Commit();
            }

            Assert.IsNotNull(savedCustomer);
            Assert.IsNotNull(savedCustomer.Orders);
            savedCustomer.Orders.ForEach(order =>
            {
                Assert.IsNotNull(order.OrderItems);
                order.OrderItems.ForEach(orderItem => Assert.IsNotNull(orderItem.Product));
            });
        }
        public void Can_perform_simple_query()
        {
            var      testData = new EFTestData(Context);
            Customer customer = null;

            testData.Batch(x => customer = x.CreateCustomer());
            using (var scope = new UnitOfWorkScope())
            {
                var savedCustomer = new EFRepository <Customer>()
                                    .First(x => x.CustomerID == customer.CustomerID);
                Assert.That(savedCustomer, Is.Not.Null);
                scope.Commit();
            }
        }
        public void Can_modify()
        {
            Customer customer = null;
            var      testData = new EFTestData(Context);

            testData.Batch(x => customer = x.CreateCustomer());
            using (var scope = new UnitOfWorkScope())
            {
                var savedCustomer = new EFRepository <Customer>()
                                    .First(x => x.CustomerID == customer.CustomerID);
                savedCustomer.FirstName = "Changed";
                scope.Commit();
            }

            testData.Refresh(customer);
            Assert.That(customer.FirstName, Is.EqualTo("Changed"));
        }
        public void can_rollback()
        {
            using (var testData = new EFTestData(Context))
            {
                Customer customer = null;
                testData.Batch(action => customer = action.CreateCustomer());

                using (new UnitOfWorkScope())
                {
                    var savedCustomer = new EFRepository<Customer,int>().Query
                        .Where(x => x.CustomerID == customer.CustomerID)
                        .First();
                    savedCustomer.LastName = "Changed";
                } //Dispose here as scope is not comitted.
                var repository=new EFRepository<Customer, int>();
                testData.Context.Refresh(RefreshMode.StoreWins, customer);
                Assert.AreNotEqual(customer.LastName, "Changed");
            }
        }
        public void can_rollback()
        {
            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer customer = null;
                testData.Batch(action => customer = action.CreateCustomer());

                using (new UnitOfWorkScope())
                {
                    var savedCustomer = new EFRepository<Customer>()
                        .Where(x => x.CustomerID == customer.CustomerID)
                        .First();
                    savedCustomer.LastName = "Changed";
                } //Dispose here as scope is not comitted.

                testData.Context<OrderEntities>().Refresh(RefreshMode.StoreWins, customer);
                Assert.That(customer.LastName, Is.Not.EqualTo("Changed"));
            }
        }
        public void Can_lazyload()
        {
            Customer customer = null;
            var testData = new EFTestData(Context);
            testData.Batch(x =>
            {
                customer = x.CreateCustomer();
                x.CreateOrderForCustomer(customer);
            });

            using (var scope = new UnitOfWorkScope())
            {
                var savedCustomer = new EFRepository<Customer>()
                    .First(x => x.CustomerID == customer.CustomerID);
                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedCustomer.Orders.Count, Is.EqualTo(1));
                scope.Commit();
            }
        }
        public void Can_Delete()
        {
            _context = new TestDbContext(this.Configuration);
            var testData        = new EFTestData(_context);
            var testDataActions = new EFTestDataActions(testData);
            var customer        = testDataActions.CreateCustomer();

            var service = this.ServiceProvider.GetService <ITestAppService>();
            var mapper  = this.ServiceProvider.GetService <IMapper>();

            var customerDto = mapper.Map <CustomerDto>(customer);
            var result      = service.Delete(customerDto);

            Customer savedCustomer = null;

            testData.Batch(action => savedCustomer = action.GetCustomerById(customer.CustomerId));

            Assert.IsNull(savedCustomer);
        }
        public void Can_delete()
        {
            Customer customer = null;
            var testData = new EFTestData(Context);
            testData.Batch(x => customer = x.CreateCustomer());
            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer>();
                var savedCustomer = repository.First(x => x.CustomerID == customer.CustomerID);
                repository.Delete(savedCustomer);
                scope.Commit();
            }

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer>();
                Assert.That(repository.FirstOrDefault(x => x.CustomerID == customer.CustomerID), Is.Null);
                scope.Commit();
            }
        }
        public void Can_query_using_specification()
        {
            var testData = new EFTestData(Context);

            testData.Batch(x =>
            {
                x.CreateCustomer(customer => customer.State = "CA");
                x.CreateCustomer(customer => customer.State = "CA");
                x.CreateCustomer(customer => customer.State = "PA");
            });

            using (var scope = new UnitOfWorkScope())
            {
                var specification = new Specification <Customer>(x => x.State == "CA");
                var results       = new EFRepository <Customer>()
                                    .Query(specification);
                Assert.That(results.Count(), Is.EqualTo(2));
                scope.Commit();
            }
        }
        public void Can_lazyload()
        {
            Customer customer = null;
            var      testData = new EFTestData(Context);

            testData.Batch(x =>
            {
                customer = x.CreateCustomer();
                x.CreateOrderForCustomer(customer);
            });

            using (var scope = new UnitOfWorkScope())
            {
                var savedCustomer = new EFRepository <Customer>()
                                    .First(x => x.CustomerID == customer.CustomerID);
                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedCustomer.Orders.Count, Is.EqualTo(1));
                scope.Commit();
            }
        }
        public void Can_attach()
        {
            Customer customer = null;
            var testData = new EFTestData(Context);
            testData.Batch(x => customer = x.CreateCustomer());
            testData.Context.Detach(customer);
            Context.Dispose();

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer,int>();
                repository.Attach(customer);
                customer.FirstName = "Changed";
                scope.Commit();
            }

            testData = new EFTestData(Context);
            customer = testData.Get<Customer>(x => x.CustomerID == customer.CustomerID);
            Assert.AreEqual(customer.FirstName, "Changed");
        }
        public void throws_when_lazyloading_outside_of_scope()
        {
            Order order    = null;
            var   testData = new EFTestData(Context);

            testData.Batch(x =>
                           order = x.CreateOrderForCustomer(x.CreateCustomer()));

            Order savedOrder = null;

            using (var scope = new UnitOfWorkScope())
            {
                savedOrder = new EFRepository <Order>()
                             .First(x => x.OrderID == order.OrderID);
                scope.Commit();
            }

            Assert.That(savedOrder, Is.Not.Null);
            Assert.Throws <ObjectDisposedException>(() => { var fname = savedOrder.Customer.FirstName; });
        }
        public void Can_Create_Async()
        {
            _context = new TestDbContext(this.Configuration);
            var testData        = new EFTestData(_context);
            var testDataActions = new EFTestDataActions(testData);
            var customer        = testDataActions.CreateCustomerStub(x => x.FirstName = "Albus");

            var service = this.ServiceProvider.GetService <ITestAppService>();
            var mapper  = this.ServiceProvider.GetService <IMapper>();

            var customerDto = mapper.Map <CustomerDto>(customer);
            var result      = service.CreateAsync(customerDto);

            Customer savedCustomer = null;

            testData.Batch(action => savedCustomer = action.GetFirstCustomer(x => x.FirstName == "Albus"));

            Assert.IsNotNull(savedCustomer);
            Assert.AreEqual(savedCustomer.FirstName, customer.FirstName);
        }
        public void Can_attach_modified_entity()
        {
            Customer customer = null;
            var testData = new EFTestData(Context);
            testData.Batch(x => customer = x.CreateCustomer());
            Context.Detach(customer);
            Context.Dispose();

            using (var scope = new UnitOfWorkScope())
            {
                customer.LastName = "Changed";
                var repository = new EFRepository<Customer>();
                repository.Attach(customer);
                scope.Commit();
            }

            Context = new PocoContext(ConnectionString);
            testData = new EFTestData(Context);
            customer = testData.Get<Customer>(x => x.CustomerID == customer.CustomerID);
            Assert.That(customer.LastName, Is.EqualTo("Changed"));
        }
        public void Can_attach()
        {
            Customer customer = null;
            var testData = new EFTestData(Context);
            testData.Batch(x => customer = x.CreateCustomer());
            Context.Detach(customer);
            Context.Dispose();

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer>();
                repository.Attach(customer);
                customer.FirstName = "Changed";
                scope.Commit();
            }

            Context = new CodeOnlyContext("SandboxCodeOnly").Context;
            testData = new EFTestData(Context);
            customer = testData.Get<Customer>(x => x.CustomerID == customer.CustomerID);
            Assert.That(customer.FirstName, Is.EqualTo("Changed"));
        }
        public void Can_delete()
        {
            Customer customer = null;
            var      testData = new EFTestData(Context);

            testData.Batch(x => customer = x.CreateCustomer());
            using (var scope = new UnitOfWorkScope())
            {
                var repository    = new EFRepository <Customer>();
                var savedCustomer = repository.First(x => x.CustomerID == customer.CustomerID);
                repository.Delete(savedCustomer);
                scope.Commit();
            }

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository <Customer>();
                Assert.That(repository.FirstOrDefault(x => x.CustomerID == customer.CustomerID), Is.Null);
                scope.Commit();
            }
        }
        public void Can_attach_modified_entity()
        {
            Customer customer = null;
            var      testData = new EFTestData(Context);

            testData.Batch(x => customer = x.CreateCustomer());
            Context.Detach(customer);
            Context.Dispose();

            using (var scope = new UnitOfWorkScope())
            {
                customer.LastName = "Changed";
                var repository = new EFRepository <Customer>();
                repository.Attach(customer);
                scope.Commit();
            }

            Context  = new PocoContext(ConnectionString);
            testData = new EFTestData(Context);
            customer = testData.Get <Customer>(x => x.CustomerID == customer.CustomerID);
            Assert.That(customer.LastName, Is.EqualTo("Changed"));
        }
        public void Can_eager_fetch()
        {
            var testData = new EFTestData(Context);

            Order order = null;
            Order savedOrder = null;

            testData.Batch(x => order = x.CreateOrderForCustomer(x.CreateCustomer()));

            using (var scope = new UnitOfWorkScope())
            {
                savedOrder = new EFRepository<Order,int>()
                    .Fetch(o => o.Customer).Query
                    .Where(x => x.OrderID == order.OrderID)
                    .SingleOrDefault();
                scope.Commit();
            }

            Assert.IsNotNull(savedOrder);
            Assert.IsNotNull(savedOrder.Customer);
            var firstName = savedOrder.Customer.FirstName;
        }
Beispiel #24
0
        public void Can_eager_fetch()
        {
            var testData = new EFTestData(Context);

            Order order      = null;
            Order savedOrder = null;

            testData.Batch(x => order = x.CreateOrderForCustomer(x.CreateCustomer()));

            using (var scope = new UnitOfWorkScope())
            {
                savedOrder = new EFRepository <Order>()
                             .Fetch(o => o.Customer)
                             .Where(x => x.OrderID == order.OrderID)
                             .SingleOrDefault();
                scope.Commit();
            }

            Assert.NotNull(savedOrder);
            Assert.NotNull(savedOrder.Customer);
            Assert.DoesNotThrow(() => { var firstName = savedOrder.Customer.FirstName; });
        }
        public void Can_attach()
        {
            Customer customer = null;
            var      testData = new EFTestData(Context);

            testData.Batch(x => customer = x.CreateCustomer());
            Context.Detach(customer);
            Context.Dispose();

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository <Customer>();
                repository.Attach(customer);
                customer.FirstName = "Changed";
                scope.Commit();
            }

            Context  = new CodeOnlyContext("SandboxCodeOnly").Context;
            testData = new EFTestData(Context);
            customer = testData.Get <Customer>(x => x.CustomerID == customer.CustomerID);
            Assert.That(customer.FirstName, Is.EqualTo("Changed"));
        }
        public void can_commit()
        {
            var customer = new Customer
            {
                FirstName = "John",
                LastName = "Doe"
            };
            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer>()
                    .Add(customer);
                scope.Commit();
            }

            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                testData.Batch(action => savedCustomer = action.GetCustomerById(customer.CustomerID));

                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedCustomer.CustomerID, Is.EqualTo(customer.CustomerID));
            }
        }
        public void Can_delete()
        {
            var customer = new Customer
            {
                FirstName = "John",
                LastName = "Doe",
            };
            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer>().Add(customer);
                scope.Commit();
            }
            Assert.That(customer.CustomerID, Is.GreaterThan(0));
            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer>();
                var savedCustomer = repository.Where(x => x.CustomerID == customer.CustomerID).First();
                repository.Delete(savedCustomer);
                scope.Commit();
            }

            //Making sure customer is deleted
            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                testData.Batch(x => savedCustomer = x.GetCustomerById(customer.CustomerID));
                Assert.That(savedCustomer, Is.Null);
            }
        }
        public void Can_modify()
        {
            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer customer = null;
                testData.Batch(x => customer = x.CreateCustomer());

                using (var scope = new UnitOfWorkScope())
                {
                    var savedCustomer = new EFRepository<Customer>()
                        .Where(x => x.CustomerID == customer.CustomerID)
                        .First();

                    savedCustomer.LastName = "Changed";
                    scope.Commit();
                }

                testData.Context<OrderEntities>().Refresh(RefreshMode.StoreWins, customer);
                Assert.That(customer.LastName, Is.EqualTo("Changed"));
            }
        }
        public void Can_lazyload()
        {
            Customer customer = null;
            var testData = new EFTestData(Context);
            testData.Batch(x =>
            {
                customer = x.CreateCustomer();
                x.CreateOrderForCustomer(customer);
            });

            //using (var scope = new UnitOfWorkScope())
            //{
                var savedCustomer = new EFRepository<Customer,int>().Query
                    .First(x => x.CustomerID == customer.CustomerID);
                Assert.IsNotNull(savedCustomer);
                Assert.AreEqual(savedCustomer.Orders.Count, 1);
            //    scope.Commit();
            //}
        }
        public void Lazyloading_when_outside_scope_throws()
        {
            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Order order = null;
                testData.Batch(x => order = x.CreateOrderForCustomer(x.CreateCustomer()));

                Order savedOrder;
                using (var scope = new UnitOfWorkScope())
                {
                    savedOrder = new EFRepository<Order>()
                        .Where(x => x.OrderID == order.OrderID)
                        .First();
                    scope.Commit();
                }
                Assert.That(savedOrder, Is.Not.Null);
                Assert.Throws<ObjectDisposedException>(() => { var customer = savedOrder.Customer; });
            }
        }
        public void throws_when_lazyloading_outside_of_scope()
        {
            Order order = null;
            var testData = new EFTestData(Context);
            testData.Batch(x =>
                order = x.CreateOrderForCustomer(x.CreateCustomer()));

            Order savedOrder = null;
            using (var scope = new UnitOfWorkScope())
            {
                savedOrder = new EFRepository<Order,int>().Query
                    .First(x => x.OrderID == order.OrderID);
                scope.Commit();
            }

            Assert.IsNotNull(savedOrder);
            var fname = savedOrder.Customer.FirstName;
        }
        public void Can_save()
        {
            var customer = new Customer
            {
                FirstName = "Jane",
                LastName = "Doe",
                StreetAddress1 = "123 Main St",
                City = "Sunset City",
                State = "LA",
                ZipCode = "12345"
            };

            using (var scope = new UnitOfWorkScope())
            {
                var repository = new EFRepository<Customer>();
                repository.Add(customer);
                scope.Commit();
            }
            Assert.That(customer.CustomerID, Is.GreaterThan(0));
            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                testData.Batch(action => savedCustomer = action.GetCustomerById(customer.CustomerID));
                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedCustomer.CustomerID, Is.EqualTo(customer.CustomerID));
            }
        }
        public void Can_query_multiple_databases()
        {
            using (var ordersTestData = new EFTestData(OrdersContextProvider()))
            using (var hrTestData = new EFTestData(HRContextProvider()))
            {
                Customer customer = null;
                SalesPerson salesPerson = null;
                ordersTestData.Batch(x => customer = x.CreateCustomer());
                hrTestData.Batch(x => salesPerson = x.CreateSalesPerson());

                //Suprisingly this does not enlist in a DTC transaction. EF is able to re-use the same connection
                //since both ObjectContext connect to the same database instance.
                using (var scope = new UnitOfWorkScope())
                {
                    var savedCustomer = new EFRepository<Customer>()
                        .Where(x => x.CustomerID == customer.CustomerID)
                        .First();

                    var savedPerson = new EFRepository<SalesPerson>()
                        .Where(x => x.Id == salesPerson.Id)
                        .First();

                    Assert.That(savedCustomer, Is.Not.Null);
                    Assert.That(savedPerson, Is.Not.Null);
                    scope.Commit();
                }
            }
        }
        public void nested_commit_with_seperate_transaction_commits_when_wrapping_scope_rollsback()
        {
            var customer = new Customer { FirstName = "Joe", LastName = "Data" };
            var order = new Order { OrderDate = DateTime.Now, ShipDate = DateTime.Now };
            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer>().Add(customer);
                using (var scope2 = new UnitOfWorkScope(TransactionMode.New))
                {
                    new EFRepository<Order>().Add(order);
                    scope2.Commit();
                }
            } //Rollback

            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                Order savedOrder = null;
                testData.Batch(actions =>
                {
                    savedCustomer = actions.GetCustomerById(customer.CustomerID);
                    savedOrder = actions.GetOrderById(order.OrderID);
                });

                Assert.That(savedCustomer, Is.Null);
                Assert.That(savedOrder, Is.Not.Null);
                Assert.That(savedOrder.OrderID, Is.EqualTo(order.OrderID));
            }
        }
        public void Can_lazyload()
        {
            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer customer = null;
                testData.Batch(x =>
                {
                    customer = x.CreateCustomer();
                    x.CreateOrderForCustomer(customer);
                });

                using (var scope = new UnitOfWorkScope())
                {
                    var savedCustomer = new EFRepository<Customer>()
                        .Where(x => x.CustomerID == customer.CustomerID)
                        .First();

                    Assert.That(savedCustomer, Is.Not.Null);
                    Assert.That(savedCustomer.Orders, Is.Not.Null);
                    Assert.DoesNotThrow(savedCustomer.Orders.Load);
                    Assert.That(savedCustomer.Orders.Count, Is.GreaterThan(0));
                    scope.Commit();
                }
            }
        }
        public void Can_query_using_specification()
        {
            var testData = new EFTestData(Context);
            testData.Batch(x =>
            {
                x.CreateCustomer(customer => customer.State = "CA");
                x.CreateCustomer(customer => customer.State = "CA");
                x.CreateCustomer(customer => customer.State = "PA");
            });

            using (var scope = new UnitOfWorkScope())
            {
                var specification = new Specification<Customer>(x => x.State == "CA");
                var results = new EFRepository<Customer>()
                    .Query(specification);
                Assert.That(results.Count(), Is.EqualTo(2));
                scope.Commit();
            }
        }
        public void throws_when_lazyloading_outside_of_scope()
        {
            Order order = null;
            var testData = new EFTestData(Context);
            testData.Batch(x =>
                order = x.CreateOrderForCustomer(x.CreateCustomer()));

            Order savedOrder = null;
            using (var scope = new UnitOfWorkScope())
            {
                savedOrder = new EFRepository<Order>()
                    .First(x => x.OrderID == order.OrderID);
                scope.Commit();
            }

            Assert.That(savedOrder, Is.Not.Null);
            Assert.Throws<ObjectDisposedException>(() => { var fname = savedOrder.Customer.FirstName; });
        }
 public void Can_perform_simple_query()
 {
     var testData = new EFTestData(Context);
     Customer customer = null;
     testData.Batch(x => customer = x.CreateCustomer());
     using (var scope = new UnitOfWorkScope())
     {
         var savedCustomer = new EFRepository<Customer>()
             .First(x => x.CustomerID == customer.CustomerID);
         Assert.That(savedCustomer, Is.Not.Null);
         scope.Commit();
     }
 }
        public void Can_modify()
        {
            Customer customer = null;
            var testData = new EFTestData(Context);
            testData.Batch(x => customer = x.CreateCustomer());
            using (var scope = new UnitOfWorkScope())
            {
                var savedCustomer = new EFRepository<Customer>()
                    .First(x => x.CustomerID == customer.CustomerID);
                savedCustomer.FirstName = "Changed";
                scope.Commit();
            }

            testData.Refresh(customer);
            Assert.That(customer.FirstName, Is.EqualTo("Changed"));
        }
        public void nested_commit_works()
        {
            var customer = new Customer { FirstName = "Joe", LastName = "Data" };
            var order = new Order { OrderDate = DateTime.Now, ShipDate = DateTime.Now };
            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer>().Add(customer);
                using (var scope2 = new UnitOfWorkScope())
                {
                    new EFRepository<Order>().Add(order);
                    scope2.Commit();
                }
                scope.Commit();
            }

            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                Order savedOrder = null;
                testData.Batch(actions =>
                {
                    savedCustomer = actions.GetCustomerById(customer.CustomerID);
                    savedOrder = actions.GetOrderById(order.OrderID);
                });

                Assert.That(savedCustomer, Is.Not.Null);
                Assert.That(savedCustomer.CustomerID, Is.EqualTo(customer.CustomerID));
                Assert.That(savedOrder, Is.Not.Null);
                Assert.That(savedOrder.OrderID, Is.EqualTo(order.OrderID));
            }
        }
        public void Can_query_using_specification()
        {
            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                testData.Batch(actions =>
                {
                    actions.CreateOrdersForCustomers(actions.CreateCustomersInState("PA", 2));
                    actions.CreateOrdersForCustomers(actions.CreateCustomersInState("DE", 5));
                    actions.CreateOrdersForCustomers(actions.CreateCustomersInState("LA", 3));
                });

                using (new UnitOfWorkScope())
                {

                    var customersInPa = new Specification<Order>(x => x.Customer.State == "DE");

                    var ordersRepository = new EFRepository<Order>();
                    var results = from order in ordersRepository.Query(customersInPa) select order;

                    Assert.That(results.Count(), Is.GreaterThan(0));
                    Assert.That(results.Count(), Is.EqualTo(5));
                }
            }
        }
        public void rollback_does_not_rollback_supressed_scope()
        {
            var customer = new Customer { FirstName = "Joe", LastName = "Data" };
            var order = new Order { OrderDate = DateTime.Now, ShipDate = DateTime.Now };
            using (var scope = new UnitOfWorkScope())
            {
                new EFRepository<Customer>().Add(customer);
                using (var scope2 = new UnitOfWorkScope(TransactionMode.Supress))
                {
                    new EFRepository<Order>().Add(order);
                    scope2.Commit();
                }
            } //Rollback.

            using (var testData = new EFTestData(OrdersContextProvider()))
            {
                Customer savedCustomer = null;
                Order savedOrder = null;
                testData.Batch(actions =>
                {
                    savedCustomer = actions.GetCustomerById(customer.CustomerID);
                    savedOrder = actions.GetOrderById(order.OrderID);
                });

                Assert.That(savedCustomer, Is.Null);
                Assert.That(savedOrder, Is.Not.Null);
            }
        }