public void EfCore_FindAll_With_Include_And_Predicate_In_Specs_LazyLoads_Email()
        {
            var repository = new EfCoreRepository <Contact, string>(dbContext);

            var findAllBySpec = new Specification <Contact>(obj => obj.ContactId == "1")
                                .And(obj => obj.EmailAddresses.Any(m => m.Email == "*****@*****.**"));

            var specification = new Specification <Contact>(obj => obj.Name == "Test User 1");

            findAllBySpec.FetchStrategy = new GenericFetchStrategy <Contact>();
            findAllBySpec.FetchStrategy
            .Include(obj => obj.EmailAddresses);

            // NOTE: This line will erase my FetchStrategy from above
            if (null != specification)
            {
                findAllBySpec = findAllBySpec.And(specification);
            }

            var contact = repository.FindAll(findAllBySpec).First();

            contact.Name.ShouldBe("Test User 1");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(2);
            contact.EmailAddresses.First().Email.ShouldBe("*****@*****.**");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(2);

            repository.FindAll(findAllBySpec).Count().ShouldBe(1);
        }
示例#2
0
        public void Delete_With_Cache_And_Ef()
        {
            var cachingStrategy = new StandardCachingStrategy <Contact, string>(cacheProvider);

            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            var options = new DbContextOptionsBuilder <TestObjectContextCore>()
                          .UseSqlite(connection)
                          .Options;

            var context = new TestObjectContextCore(options);

            context.Database.EnsureCreated();

            var repository = new EfCoreRepository <Contact, string>(context, cachingStrategy);

            repository.Add(new Contact()
            {
                ContactId = "1", Name = "Contact1"
            });

            repository = new EfCoreRepository <Contact, string>(context, cachingStrategy);
            repository.Get("1");
            repository.CacheUsed.ShouldBeTrue();
            repository.Delete("1");
        }
        public async Task Can_Update_Customer()
        {
            //Arrange
            var testCustomer = new Customer()
            {
                Name = "First Customer"
            };

            using (var context = new AppDbContext(_contextOptions))
            {
                //Act
                var repository         = new EfCoreRepository <Customer>(context);
                var insertedCustomerId = await repository.InsertAsync(testCustomer);

                var insertedCustomer = await repository.GetByIdAsync(insertedCustomerId);

                insertedCustomer.Name = "First Updated Customer";
                await repository.UpdateAsync(insertedCustomer);

                //Assert
                var updatedCustomer = await repository.GetByIdAsync(insertedCustomerId);

                Assert.Equal(insertedCustomer.Name, updatedCustomer.Name);
            }
        }
示例#4
0
        public void Delete_Loop_With_Cache_And_Ef()
        {
            var cachingStrategy = new StandardCachingStrategy <Contact, string>(cacheProvider);
            var connection      = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            var options = new DbContextOptionsBuilder <TestObjectContextCore>()
                          .UseSqlite(connection)
                          .Options;

            var context = new TestObjectContextCore(options);

            context.Database.EnsureCreated();

            var repository = new EfCoreRepository <Contact, string>(context, cachingStrategy);

            repository.Add(new Contact()
            {
                ContactId = "1", Name = "Contact1", ContactTypeId = 1
            });
            repository.Add(new Contact()
            {
                ContactId = "2", Name = "Contact2", ContactTypeId = 2
            });
            repository.Add(new Contact()
            {
                ContactId = "3", Name = "Contact3", ContactTypeId = 2
            });
            repository.FindAll(x => x.ContactTypeId == 2);

            repository = new EfCoreRepository <Contact, string>(new TestObjectContextCore(options), cachingStrategy);

            repository.Delete(x => x.ContactTypeId == 2);
        }
        public void CompoundKeyRepository_Should_Work()
        {
            var dbPath = EfDataDirectoryFactory.Build();
            ICompoundKeyRepository <User, string, int> repository = new EfCoreRepository <User, string, int>(context);

            repository.Add(new User {
                Username = "******", Age = 21, FullName = "Jeff - 21"
            });
            repository.Add(new User {
                Username = "******", Age = 31, FullName = "Jeff - 31"
            });
            repository.Add(new User {
                Username = "******", Age = 41, FullName = "Jeff - 41"
            });

            repository.Add(new User {
                Username = "******", Age = 31, FullName = "Ben - 31"
            });
            repository.Add(new User {
                Username = "******", Age = 41, FullName = "Ben - 41"
            });
            repository.Add(new User {
                Username = "******", Age = 51, FullName = "Ben - 51"
            });

            repository.Get("jeff", 31).FullName.ShouldBe("Jeff - 31");
            repository.Get("ben", 31).FullName.ShouldBe("Ben - 31");
            repository.Get("jeff", 41).FullName.ShouldBe("Jeff - 41");

            repository.FindAll(x => x.Age == 31).Count().ShouldBe(2);
        }
        public void EfCoreGetSateShouldBeUnchanged()
        {
            var repository = new EfCoreRepository <Contact, string>(dbContext);

            var firstContact = repository.Get("1");

            dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
        }
示例#7
0
        public void TestAsyncRequest()
        {
            var repo = new EfCoreRepository <Contact>(context);

            var contacts = repo.GetAll().ToAsyncEnumerable().ToList();

            contacts.Result.Count.ShouldBe(0);
        }
        public void EfCoreGetAllWithStrategySateShouldBeUnchanged()
        {
            var repository = new EfCoreRepository <Contact, string>(dbContext);

            var strat = new GenericFetchStrategy <Contact>();

            var firstContact = repository.GetAll(strat).First();

            dbContext.Entry(firstContact).State.ShouldBe(EntityState.Unchanged);
        }
        public async Task Can_Update_Order()
        {
            //Arrange
            var testCustomer = new Customer()
            {
                Name = "Test Customer to Order"
            };
            var testBook = new Book()
            {
                Name = "Test Book to Order", Price = 15.50M,
            };

            using (var context = new AppDbContext(_contextOptions))
            {
                var customerRepository = new EfCoreRepository <Customer>(context);
                await customerRepository.InsertAsync(testCustomer);

                var bookRepository = new EfCoreRepository <Book>(context);
                await bookRepository.InsertAsync(testBook);
            }

            var testOrder = new Order()
            {
                CustomerId    = testCustomer.Id,
                OrderNote     = "Test Order",
                OrderProducts = new List <OrderProduct>()
                {
                    new OrderProduct()
                    {
                        BookId     = testBook.Id,
                        TotalPrice = testBook.Price * 5
                    }
                }
            };

            testOrder.OrderTotal = testOrder.OrderProducts.Sum(op => op.TotalPrice);

            using (var context = new AppDbContext(_contextOptions))
            {
                //Act
                var repository      = new EfCoreRepository <Order>(context);
                var insertedOrderId = await repository.InsertAsync(testOrder);

                var insertedOrder = await repository.GetByIdAsync(insertedOrderId);

                insertedOrder.OrderNote = "Test Updated Order";
                await repository.UpdateAsync(insertedOrder);

                //Assert
                var updatedOrder = await repository.GetByIdAsync(insertedOrderId);

                Assert.Equal(insertedOrder.OrderNote, updatedOrder.OrderNote);
            }
        }
        public void EfCore_GetAll_With_Text_Include_LazyLoads_Email()
        {
            var repository = new EfCoreRepository <Contact, string>(dbContext);

            var contact = repository.GetAll("EmailAddresses").First();

            contact.Name.ShouldBe("Test User 1");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(2);
            contact.EmailAddresses.First().Email.ShouldBe("*****@*****.**");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(2);
        }
        public void EfCore_GetAll_Without_Includes_LazyLoads_Email()
        {
            var repository = new EfCoreRepository <Contact, string>(dbContext);

            var contact = repository.GetAll().First();

            contact.Name.ShouldBe("Test User 1");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(1);
            contact.EmailAddresses.First().Email.ShouldBe("*****@*****.**");
            // dbContext.QueryLog.Count(filterSelects).ShouldBe(2); may be that dbcontext is disposed and the successive queries are not logged, quieries does not contains email so query was made in a lazy way but after.
        }
示例#12
0
        public void EfCoreGetWithStrategyAsNoTrackingSateShouldBeUnchanged()
        {
            var repository = new EfCoreRepository <Contact, string>(dbContext);

            var strat = new GenericFetchStrategy <Contact>()
                        .AsNoTracking();

            var firstContact = repository.Get("1", strat);

            dbContext.Entry(firstContact).State.ShouldBe(EntityState.Detached);
        }
        public void EfCore_GetAll_With_Text_Include_And_Pagination_LazyLoads_Email()
        {
            var repository = new EfCoreRepository <Contact, string>(dbContext);

            var pagination = new PagingOptions <Contact>(1, 4, "ContactId");

            var contact = repository.GetAll(pagination, "EmailAddresses").First();

            contact.Name.ShouldBe("Test User 1");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(3); // first query is count for total records
            contact.EmailAddresses.First().Email.ShouldBe("*****@*****.**");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(3);
        }
        public void EfCore_GetAll_With_Includes_In_Strategy_LazyLoads_Email()
        {
            var repository = new EfCoreRepository <Contact, string>(dbContext);
            var strategy   = new GenericFetchStrategy <Contact>();

            strategy.Include(x => x.EmailAddresses);

            var contact = repository.GetAll(strategy).First();

            contact.Name.ShouldBe("Test User 1");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(2);
            contact.EmailAddresses.First().Email.ShouldBe("*****@*****.**");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(2);
        }
示例#15
0
        public void Join_GetAll_Should_Return_All_Items_EfCore()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            var options = new DbContextOptionsBuilder <TestObjectContextCore>()
                          .UseSqlite(connection)
                          .Options;

            // Create the schema in the database
            var context = new TestObjectContextCore(options);

            context.Database.EnsureCreated();
            var efCoreRepository = new EfCoreRepository <User, string, int>(context);

            Join_GetAll_Should_Return_All_Items(efCoreRepository);
        }
示例#16
0
        public void EfCore_GetAll_With_Selector_Selects_Only_Specified_Columns()
        {
            var repository = new EfCoreRepository <EmailAddress, int>(dbContext);

            var emailAddress = repository.GetAll(s => new { s.ContactId, s.EmailAddressId, s.Email }).First();

            emailAddress.Email.ShouldStartWith("omar.piani.");
            dbContext.QueryLog.Count(filterSelects).ShouldBe(1, "A select was executed");
            string queryLogElement = dbContext.QueryLog.Where(filterSelects).First();
            Regex  regex           = new Regex("SELECT(.+)FROM.+", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            var    selectMatches   = regex.Matches(queryLogElement);

            selectMatches.Count.ShouldBe(1, "A regex match was found for the select pattern");
            var selectPart = selectMatches[0].Groups[1].Value;

            selectPart.ShouldContain(nameof(EmailAddress.ContactId), "ContactId was selected");
            selectPart.ShouldContain(nameof(EmailAddress.EmailAddressId), "EmailAddressId was selected");
            selectPart.ShouldContain(nameof(EmailAddress.Email), "Email was selected");
            selectPart.ShouldNotContain(nameof(EmailAddress.Label), "Label was not selected");
        }
        public async Task Can_Add_Customer()
        {
            //Arrange
            var testCustomer = new Customer()
            {
                Name = "First Customer"
            };

            using (var context = new AppDbContext(_contextOptions))
            {
                //Act
                var repository = new EfCoreRepository <Customer>(context);
                await repository.InsertAsync(testCustomer);

                //Assert
                var customer = await repository.GetByIdAsync(testCustomer.Id);

                Assert.Equal(testCustomer.Id, customer.Id);
                Assert.Equal(testCustomer.Name, customer.Name);
            }
        }
        public async Task Can_Delete_Customer()
        {
            //Arrange
            var testCustomer = new Customer()
            {
                Name = "İlk Bölüm"
            };

            using (var context = new AppDbContext(_contextOptions))
            {
                //Act
                var repository = new EfCoreRepository <Customer>(context);
                await repository.InsertAsync(testCustomer);

                await repository.DeleteAsync(testCustomer);

                //Assert
                var deletedCustomer = await repository.GetByIdAsync(testCustomer.Id);

                Assert.Null(deletedCustomer);
            }
        }
 public EfCoreController(EfCoreRepository repository)
 {
     this._repository = repository;
 }
 public CookieBakingController(EfCoreRepository <Order> orderRepository,
                               EfCoreRepository <CookieBaking> cookieBakingRepository)
 {
     _orderRepository        = orderRepository;
     _cookieBakingRepository = cookieBakingRepository;
 }