Пример #1
0
            public void AddsNonExistingEntity()
            {
                using (var dbContext = new TestDbContextContainer())
                {
                    using (var repository = new DbContextCustomerRepository(dbContext))
                    {
                        var customer = EFTestHelper.CreateCustomer(1235);

                        repository.Attach(customer);

                        dbContext.SaveChanges();

                        var fetchedCustomer = repository.GetByKey(1235);
                        Assert.AreEqual(customer, fetchedCustomer);
                    }
                }
            }
Пример #2
0
            public void CorrectlySavesChangesWhenNotInTransaction()
            {
                using (var uow = new UnitOfWork <TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository <IDbContextCustomerRepository>();

                    var customer = EFTestHelper.CreateCustomer(401);
                    customerRepository.Add(customer);

                    uow.SaveChanges();
                }

                using (var uow = new UnitOfWork <TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository <IDbContextCustomerRepository>();

                    var customer = customerRepository.GetByKey(401);

                    Assert.IsNotNull(customer);
                }
            }
Пример #3
0
            public void CorrectlyCommitsTransaction()
            {
                using (var uow = new UnitOfWork <TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository <IDbContextCustomerRepository>();
                    var productRepository  = uow.GetRepository <IDbContextProductRepository>();
                    var orderRepository    = uow.GetRepository <IDbContextOrderRepository>();

                    uow.BeginTransaction();

                    var customer = EFTestHelper.CreateCustomer(451);
                    customerRepository.Add(customer);

                    var product = EFTestHelper.CreateProduct(451);
                    productRepository.Add(product);

                    var order = new DbContextOrder {
                        OrderCreated = DateTime.Now, Amount = 1, CustomerId = 451, ProductId = 451
                    };
                    orderRepository.Add(order);

                    uow.CommitTransaction();
                }

                using (var uow = new UnitOfWork <TestDbContextContainer>())
                {
                    var customerRepository = uow.GetRepository <IDbContextCustomerRepository>();
                    var productRepository  = uow.GetRepository <IDbContextProductRepository>();
                    var orderRepository    = uow.GetRepository <IDbContextOrderRepository>();

                    var customer = customerRepository.GetByKey(451);
                    Assert.IsNotNull(customer);

                    var product = productRepository.GetByKey(451);
                    Assert.IsNotNull(product);

                    var order = orderRepository.FirstOrDefault(x => x.CustomerId == 451 && x.ProductId == 451);
                    Assert.IsNotNull(order);
                }
            }