public void CompoundKeyRepositoryNoGenerics_Should_Work() { var dbPath = EfDataDirectoryFactory.Build(); Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); ICompoundKeyRepository <User> repository = new Ef5CompoundKeyRepository <User>(new TestObjectEntities("Data Source=" + dbPath)); 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.ShouldEqual("Jeff - 31"); repository.Get("ben", 31).FullName.ShouldEqual("Ben - 31"); repository.Get("jeff", 41).FullName.ShouldEqual("Jeff - 41"); repository.FindAll(x => x.Age == 31).Count().ShouldEqual(2); }
public void SetupRepository() { queries = new List <string>(); var dbPath = EfDataDirectoryFactory.Build(); dbContext = new TestObjectEntities("Data Source=" + dbPath); const int totalItems = 5; for (int i = 1; i <= totalItems; i++) { dbContext.Contacts.Add( new Contact { ContactId = i.ToString(), Name = "Test User " + i, EmailAddresses = new List <EmailAddress> { new EmailAddress { ContactId = i.ToString(), EmailAddressId = i, Email = "omar.piani." + i.ToString() + "@email.com", Label = "omar.piani." + i.ToString() } } }); } dbContext.SaveChanges(); // reistantiate in order to lose caches dbContext = new TestObjectEntities("Data Source=" + dbPath); }
public void CompoundKeyRepositoryNoGenerics_Should_Work() { var dbPath = EfDataDirectoryFactory.Build(); ICompoundKeyRepository <User> repository = new EfCoreCompoundKeyRepository <User>(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 CompoundKeyRepository_Should_Work() { var dbPath = EfDataDirectoryFactory.Build(); ICompoundKeyRepository <User, string, int> repository = new EfRepository <User, string, int>(new TestObjectEntities("Data Source=" + dbPath)); 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.ShouldEqual("Jeff - 31"); repository.Get("ben", 31).FullName.ShouldEqual("Ben - 31"); repository.Get("jeff", 41).FullName.ShouldEqual("Jeff - 41"); repository.FindAll(x => x.Age == 31).Count().ShouldEqual(2); }
public void Delete_Loop_With_Cache_And_Ef() { var cachingStrategy = new StandardCachingStrategy <Contact, string>(); var dbPath = EfDataDirectoryFactory.Build(); Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); var repository = new EfRepository <Contact, string>(new TestObjectEntities("Data Source=" + dbPath), 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 EfRepository <Contact, string>(new TestObjectEntities("Data Source=" + dbPath), cachingStrategy); repository.Delete(x => x.ContactTypeId == 2); }
public void Add_Should_Save_And_Assign_1_To_InMemory_Int_Id() { var dbPath = EfDataDirectoryFactory.Build(); var repository = new InMemoryRepository <ContactInt, int>(); var contact = new ContactInt { Name = "Test User" }; repository.Add(contact); contact.ContactIntId.ShouldEqual(1); }
public void Add_Should_Save_And_Assign_1_To_Ef_Int_Id_When_GenerateKeyOnAdd_Is_False() { var dbPath = EfDataDirectoryFactory.Build(); var repository = new EfRepository <ContactInt, int>(new TestObjectEntities("Data Source=" + dbPath)); var contact = new ContactInt { Name = "Test User" }; repository.GenerateKeyOnAdd = false; repository.Add(contact); contact.ContactIntId.ShouldEqual(1); }
public void Setup() { var dbPath = EfDataDirectoryFactory.Build(); // structure map container = new Container(x => { x.AddRegistry(new StructureMapRegistry(dbPath)); x.ForRepositoriesUseSharpRepository(); }); RepositoryDependencyResolver.SetDependencyResolver(new StructureMapRepositoryDependencyResolver(container)); }
public void Add_Should_Save_But_Not_Assign_New_InMemory_Int_Id_When_GenerateKeyOnAdd_Is_False() { var dbPath = EfDataDirectoryFactory.Build(); var repository = new InMemoryRepository <ContactInt, int>(); var contact = new ContactInt { Name = "Test User" }; repository.GenerateKeyOnAdd = false; repository.Add(contact); contact.ContactIntId.ShouldEqual(0); }
public void LazyLoad_Set_To_False() { var dbPath = EfDataDirectoryFactory.Build(); Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); var dbContext = new TestObjectEntities("Data Source=" + dbPath); dbContext.Configuration.LazyLoadingEnabled = false; var repos = new MyEfRepository(dbContext); repos.LazyLoadValue.ShouldBeFalse(); }
public void Setup() { var dbPath = EfDataDirectoryFactory.Build(); Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); // structure map container = new Container(x => { x.AddRegistry(new StructureMapRegistry(dbPath)); x.ForRepositoriesUseSharpRepository(); }); RepositoryDependencyResolver.SetDependencyResolver(new StructureMapRepositoryDependencyResolver(container)); }
public void Delete_With_Cache_And_Ef() { var cachingStrategy = new StandardCachingStrategy <Contact, string>(); var dbPath = EfDataDirectoryFactory.Build(); var repository = new EfRepository <Contact, string>(new TestObjectEntities("Data Source=" + dbPath), cachingStrategy); repository.Add(new Contact() { ContactId = "1", Name = "Contact1" }); repository = new EfRepository <Contact, string>(new TestObjectEntities("Data Source=" + dbPath), cachingStrategy); repository.Get("1"); repository.CacheUsed.ShouldBeTrue(); repository.Delete("1"); }
public void Deleted_Entity_With_Nullable_PartitionKey_Should_Not_Be_Returned_From_Cache() { var dbPath = EfDataDirectoryFactory.Build(); Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); // Standard partition cache and expression. var strategy = new StandardCachingStrategyWithPartition <Node, int, int?>(new InMemoryCachingProvider(), n => n.ParentId); // Standard EF repo. IRepository <Node, int> repository = new EfRepository <Node, int>(new TestObjectEntities("Data Source=" + dbPath), strategy); // Create a root node (ParentId = null). var rootNode = new Node() { ParentId = null, Name = "Root" }; repository.Add(rootNode); // Note: If we dont assign an int? here then TryPartitionValue in StandardCachingStrategyBase cannot find the partition key in the expression // because there would be a cast made in the expression which breaks partition key discovery. int?rootNodeId = rootNode.Id; // Create a child node referencing the root node as it's parent. var childNode = new Node() { ParentId = rootNodeId, Name = "Child" }; repository.Add(childNode); // Find the child nodes via the parentId, this will cause the cache to store the child node. repository.FindAll(n => n.ParentId == rootNodeId).Count().ShouldEqual(1); // Deleting the child node should also increment the partition generation. repository.Delete(childNode); // Subsequent query should not return the child node. repository.FindAll(n => n.ParentId == rootNodeId).Count().ShouldEqual(0, "Deleted entity returned from cache."); }
public void SetupRepository() { var dbPath = EfDataDirectoryFactory.Build(); var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <TestObjectContextCore>() .UseSqlite(connection) .Options; using (dbContext = new TestObjectContextCore(options)) { dbContext.Database.EnsureCreated(); const int totalItems = 5; for (int i = 1; i <= totalItems; i++) { dbContext.Contacts.Add( new Contact { ContactId = i.ToString(), Name = "Test User " + i, EmailAddresses = new List <EmailAddress> { new EmailAddress { ContactId = i.ToString(), EmailAddressId = i, Email = "omar.piani." + i.ToString() + "@email.com", Label = "omar.piani." + i.ToString() } } }); } dbContext.SaveChanges(); } // reistantiate in order to lose caches dbContext = new TestObjectContextCore(options); }