public void Detached_AssociatedEntityWherePreviousValueIsNotNewValue() { Models.Project project; Models.Manager coord; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); coord = context.Managers .Single(p => p.PartKey == "manager1" && p.PartKey2 == 1); } // Simulate detach project.LeadCoordinator = coord; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .AssociatedEntity(p => p.LeadCoordinator)); context.SaveChanges(); } using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.PartKey == coord.PartKey); } }
public void Detached_MarksAssociatedRelationAsChangedEvenIfEntitiesAreUnchanged() { Models.Project project1; Models.Manager manager1; using (var context = new TestDbContext()) { project1 = context.Projects.Include(m => m.LeadCoordinator).Single(p => p.Id == 1); manager1 = context.Managers.First(); } // Simulate detach project1.LeadCoordinator = manager1; using (var context = new TestDbContext()) { context.UpdateGraph(project1, p => p.AssociatedEntity(e => e.LeadCoordinator)); context.SaveChanges(); } using (var context = new TestDbContext()) { var coordinator = context.Projects.Include(m => m.LeadCoordinator).Single(p => p.Id == 1).LeadCoordinator; Assert.IsNotNull(coordinator); Assert.AreEqual(coordinator.PartKey, manager1.PartKey); // Compare Keys because the instances are not equals. Assert.AreEqual(coordinator.PartKey2, manager1.PartKey2); // Compare Keys because the instances are not equals. } }
public void Detached_OwnedEntityNewEntity() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); } // Simulate detach project.LeadCoordinator = new Models.Manager { FirstName = "Br", PartKey = "TER", PartKey2 = 2 }; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .OwnedEntity(p => p.LeadCoordinator)); context.SaveChanges(); } using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.PartKey == "TER"); } }
public void Detached_AssociatedEntityValuesShouldNotBeUpdated() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); } // Simulate detach project.LeadCoordinator.FirstName = "Larry"; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .AssociatedEntity(p => p.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.FirstName != "Larry"); } }
public void Detached_AssociatedCollectionAdd() { // don't know what to do about this yet.. Models.Project project1; Models.Company company2; using (var context = new TestDbContext()) { project1 = context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2); company2 = context.Companies.Single(p => p.Id == 2); } // Simulate detach project1.Stakeholders.Add(company2); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project1, map => map .AssociatedCollection(p => p.Stakeholders)); context.SaveChanges(); } using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2) .Stakeholders.Count == 2); } }
public void Detached_OwnedEntityRemoveEntity() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); } // Simulate detach project.LeadCoordinator = null; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .OwnedEntity(p => p.LeadCoordinator)); context.SaveChanges(); } using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator == null); } }
public void Attached_OwnedCollectionRemove() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); //} // Simulate detach //company1.Contacts.Remove(company1.Contacts.First()); context.Entry(company1.Contacts.First()).State = EntityState.Deleted; //using (var context = new TestDbContext()) //{ // Setup mapping //context.Entry(company1).State = EntityState.Modified; context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(p => p.Infos))); context.SaveChanges(); Assert.IsTrue(context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2) .Contacts.Count == 0); } }
public void Detached_AssociatedCollectionRemove() { Models.Project project1; using (var context = new TestDbContext()) { project1 = context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2); } // Simulate detach var company = project1.Stakeholders.First(); project1.Stakeholders.Remove(company); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project1, map => map .AssociatedCollection(p => p.Stakeholders)); context.SaveChanges(); } using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2) .Stakeholders.Count == 0); // Ensure does not delete non owned entity Assert.IsTrue(context.Companies.Any(p => p.Id == company.Id)); } }
public void Detached_AssociatedCollectionsEntitiesValuesShouldNotBeUpdated() { Models.Project project1; using (var context = new TestDbContext()) { project1 = context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2); } // Simulate detach var company = project1.Stakeholders.First(); company.Name = "TEST OVERWRITE NAME"; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project1, map => map .AssociatedCollection(p => p.Stakeholders)); context.SaveChanges(); } using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2) .Stakeholders.First().Name != "TEST OVERWRITE NAME"); } }
public void Attached_OwnedEntityUpdateValues() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); //} // Simulate detach project.LeadCoordinator.FirstName = "Tada"; //using (var context = new TestDbContext()) //{ // Setup mapping context.UpdateGraph(project, map => map .OwnedEntity(p => p.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.FirstName == "Tada"); } }
public void Attached_AssociatedEntityValuesShouldNotBeUpdated() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); //} // Simulate detach project.LeadCoordinator.FirstName = "Larry"; //using (var context = new TestDbContext()) //{ // Setup mapping context.UpdateGraph(project, map => map .AssociatedEntity(p => p.LeadCoordinator)); Assert.IsTrue(context.ChangeTracker.Entries().All(p => p.State == System.Data.EntityState.Unchanged)); context.SaveChanges(); } using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.FirstName != "Larry"); } }
public void Attached_AssociatedEntityWhereNewValueIsNull() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); //} // Simulate detach project.LeadCoordinator = null; //using (var context = new TestDbContext()) //{ // Setup mapping context.UpdateGraph(project, map => map .AssociatedEntity(p => p.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator == null); } }
public void Detached_OwnedCollectionRemove() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); } // Simulate detach company1.Contacts.Remove(company1.Contacts.First()); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(p => p.Infos))); context.SaveChanges(); Assert.IsTrue(context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2) .Contacts.Count == 0); } }
public void Detached_OwnedCollectionWithOwnedCollection() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .First(); } // Simulate detach company1.Contacts.First().Infos.First().Email = "testeremail"; company1.Contacts.First().Infos.Add(new Models.ContactInfo { Description = "Test", Email = "*****@*****.**" }); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(m => m.Infos))); context.SaveChanges(); var value = context.Companies.Include(p => p.Contacts.Select(m => m.Infos)) .First(); Assert.IsTrue(value.Contacts.First().Infos.Count == 2); Assert.IsTrue(value.Contacts.First().Infos.First().Email == "testeremail"); } }
public void OwnedCollectionUpdate() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts) .Single(p => p.Id == 2); } // Simulate detach company1.Name = "Company #1"; // Change from Company 1 to Company #1 company1.Contacts.First().FirstName = "Bobby"; // change to bobby using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts)); context.SaveChanges(); Assert.IsTrue(context.Companies .Include(p => p.Contacts) .Single(p => p.Id == 2) .Contacts.First() .FirstName == "Bobby"); Assert.IsTrue(context.Companies .Include(p => p.Contacts) .Single(p => p.Id == 2) .Contacts.First() .LastName == "Jones"); } }
public void Detached_OwnedCollectionAddRemoveUpdate() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); company1.Contacts.Add(new Models.CompanyContact { FirstName = "Hello", LastName = "Test" }); context.SaveChanges(); } // Simulate detach // Update, remove and add company1.Name = "Company #1"; // Change from Company 1 to Company #1 string originalname = company1.Contacts.First().FirstName; company1.Contacts.First().FirstName = "Terrrrrry"; company1.Contacts.Remove(company1.Contacts.Skip(1).First()); company1.Contacts.Add(new Models.CompanyContact { FirstName = "Charlie", LastName = "Sheen", Infos = new List <Models.ContactInfo> { new Models.ContactInfo { PhoneNumber = "123456789", Description = "Home" } } }); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(p => p.Infos))); context.SaveChanges(); var test = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); Assert.IsTrue(test.Contacts.Count == 2); Assert.IsTrue(test.Contacts.First().FirstName == "Terrrrrry"); Assert.IsTrue(test.Contacts.Skip(1).First().FirstName == "Charlie"); } }
public void Detached_DoesNotUpdateEntityIfNoChangesHaveBeenMade() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies.Single(p => p.Id == 2); } // Simulate detach using (var context = new TestDbContext()) { context.UpdateGraph(company1, null); Assert.IsTrue(context.ChangeTracker.Entries().All(p => p.State == System.Data.EntityState.Unchanged)); } }
public void Attached_BaseEntityUpdate() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies.Single(p => p.Id == 2); //} // Simulate detach company1.Name = "Company #1"; // Change from Company 1 to Company #1 //using (var context = new TestDbContext()) //{ context.UpdateGraph(company1, null); context.SaveChanges(); Assert.IsTrue(context.Companies.Single(p => p.Id == 2).Name == "Company #1"); } }
public void Attached_MarksAssociatedRelationAsChangedEvenIfEntitiesAreUnchanged() { Models.Project project1; Models.Manager manager1; using (var context = new TestDbContext()) { project1 = context.Projects.Include(m => m.LeadCoordinator).Single(p => p.Id == 1); manager1 = context.Managers.First(); //} // Simulate detach project1.LeadCoordinator = manager1; //using (var context = new TestDbContext()) //{ context.UpdateGraph(project1, p => p.AssociatedEntity(e => e.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects.Include(m => m.LeadCoordinator).Single(p => p.Id == 1).LeadCoordinator == manager1); } }
public void Attached_EnsureWeCanUseCyclicRelationsOnOwnedCollections() { Models.Manager manager; using (var context = new TestDbContext()) { manager = context.Managers.Include(p => p.Employees).First(); //} // Simulate disconnect var newEmployee = new Models.Employee { Key = "assdf", FirstName = "Test Employee", Manager = manager }; manager.Employees.Add(newEmployee); //using (var context = new TestDbContext()) //{ context.UpdateGraph(manager, m1 => m1.OwnedCollection(o => o.Employees)); context.SaveChanges(); Assert.IsTrue(context.Employees.Include(p => p.Manager).Single(p => p.Key == "assdf").Manager.FirstName == manager.FirstName); } }
public void Detached_OwnedCollectionAdd() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); } // Simulate detach company1.Name = "Company #1"; // Change from Company 1 to Company #1 company1.Contacts.Add(new Models.CompanyContact { FirstName = "Charlie", LastName = "Sheen", Infos = new List <Models.ContactInfo> { new Models.ContactInfo { PhoneNumber = "123456789", Description = "Home" } } }); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(p => p.Infos))); context.SaveChanges(); Assert.IsTrue(context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2) .Contacts.Count == 2); Assert.IsTrue(context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2) .Contacts.Any(p => p.LastName == "Sheen")); } }
public void Attached_AssociatedEntityValuesForNewValueShouldNotBeUpdated() { Models.Project project; Models.Manager coord; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); coord = context.Managers .Single(p => p.PartKey == "manager1" && p.PartKey2 == 1); //} // Simulate detach project.LeadCoordinator = coord; coord.FirstName = "Larry"; //using (var context = new TestDbContext()) //{ // Setup mapping context.UpdateGraph(project, map => map .AssociatedEntity(p => p.LeadCoordinator)); context.SaveChanges(); } // Force reload of DB entities. // note can also be done with GraphDiffConfiguration.ReloadAssociatedEntitiesWhenAttached. using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.FirstName == "Trent"); } }
public void OwnedMultipleLevelCollectionMappingWithAssociatedReload() { Models.MultiLevelTest multiLevelTest; Models.Hobby hobby; using (var context = new TestDbContext()) { multiLevelTest = context.MultiLevelTest.Add(new Models.MultiLevelTest { Managers = new[] // test arrays as well { new Models.Manager { PartKey = "xxx", PartKey2 = 2, Employees = new List<Models.Employee> { new Models.Employee { Key = "xsdf", FirstName = "Asdf", Hobbies = new List<Models.Hobby> { new Models.Hobby { HobbyType = "Test hobby type" } } } } } } }); hobby = context.Hobbies.Add(new Models.Hobby { HobbyType = "Skiing" }); context.SaveChanges(); } // Simulate detach // Graph changes // Should not update changes to hobby hobby.HobbyType = "Something Else"; // Update changes to manager var manager = multiLevelTest.Managers.First(); manager.FirstName = "Tester"; // Update changes to employees var employeeToUpdate = manager.Employees.First(); employeeToUpdate.Hobbies.Clear(); employeeToUpdate.Hobbies.Add(hobby); manager.Employees.Add(new Models.Employee { FirstName = "Tim", Key = "Tim1", Manager = multiLevelTest.Managers.First() }); using (var context = new TestDbContext()) { GraphDiffConfiguration.ReloadAssociatedEntitiesWhenAttached = true; // Setup mapping context.UpdateGraph(multiLevelTest, map => map .OwnedCollection(x => x.Managers, withx => withx .AssociatedCollection(pro => pro.Projects) .OwnedCollection(p => p.Employees, with => with .AssociatedCollection(m => m.Hobbies) .OwnedEntity(m => m.Locker)))); context.SaveChanges(); GraphDiffConfiguration.ReloadAssociatedEntitiesWhenAttached = false; var result = context.MultiLevelTest .Include("Managers.Employees.Hobbies") .Include("Managers.Employees.Locker") .Include("Managers.Projects") .First(); var updateManager = result.Managers.Single(p => p.PartKey == manager.PartKey && p.PartKey2 == manager.PartKey2); var updateEmployee = updateManager.Employees.Single(p => p.Key == employeeToUpdate.Key); var updateHobby = context.Hobbies.Single(p => p.Id == hobby.Id); Assert.IsTrue(updateManager.Employees.Count() == 2); Assert.IsTrue(result.Managers.First().FirstName == "Tester"); Assert.IsTrue(updateEmployee.Hobbies.Count() == 1); Assert.IsTrue(updateEmployee.Hobbies.First().HobbyType == "Skiing"); Assert.IsTrue(updateHobby.HobbyType == "Skiing"); Assert.IsTrue(result.Managers.First().Employees.Any(p => p.Key == "Tim1")); } }
public void MarksAssociatedRelationAsChangedEvenIfEntitiesAreUnchanged() { Models.Project project1; Models.Manager manager1; using (var context = new TestDbContext()) { project1 = context.Projects.Include(m => m.LeadCoordinator).Single(p => p.Id == 1); manager1 = context.Managers.First(); } // Simulate detach project1.LeadCoordinator = manager1; using (var context = new TestDbContext()) { context.UpdateGraph(project1, p => p.AssociatedEntity(e => e.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects.Include(m => m.LeadCoordinator).Single(p => p.Id == 1).LeadCoordinator == manager1); } }
public void AssociatedCollectionRemove() { Models.Project project1; using (var context = new TestDbContext()) { project1 = context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2); } // Simulate detach var company = project1.Stakeholders.First(); project1.Stakeholders.Remove(company); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project1, map => map .AssociatedCollection(p => p.Stakeholders)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2) .Stakeholders.Count == 0); // Ensure does not delete non owned entity Assert.IsTrue(context.Companies.Any(p => p.Id == company.Id)); } }
public void AssociatedCollectionAdd() { // don't know what to do about this yet.. Models.Project project1; Models.Company company2; using (var context = new TestDbContext()) { project1 = context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2); company2 = context.Companies.Single(p => p.Id == 2); } // Simulate detach project1.Stakeholders.Add(company2); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project1, map => map .AssociatedCollection(p => p.Stakeholders)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2) .Stakeholders.Count == 2); } }
public void AssociatedEntityValuesForNewValueShouldNotBeUpdated() { Models.Project project; Models.Manager coord; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); coord = context.Managers .Single(p => p.PartKey == "manager1" && p.PartKey2 == 1); } // Simulate detach project.LeadCoordinator = coord; coord.FirstName = "Larry"; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .AssociatedEntity(p => p.LeadCoordinator)); context.SaveChanges(); } // Force reload of DB entities. // note can also be done with GraphDiffConfiguration.ReloadAssociatedEntitiesWhenAttached. using (var context = new TestDbContext()) { Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.FirstName == "Trent"); } }
public void AssociatedCollectionsEntitiesValuesShouldNotBeUpdated() { Models.Project project1; using (var context = new TestDbContext()) { project1 = context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2); } // Simulate detach var company = project1.Stakeholders.First(); company.Name = "TEST OVERWRITE NAME"; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project1, map => map .AssociatedCollection(p => p.Stakeholders)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.Stakeholders) .Single(p => p.Id == 2) .Stakeholders.First().Name != "TEST OVERWRITE NAME"); } }
public void AssociatedEntityWherePreviousValueWasNull() { Models.Project project; Models.Manager coord; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 1); coord = context.Managers .Single(p => p.PartKey == "manager1" && p.PartKey2 == 1); } // Simulate detach project.LeadCoordinator = coord; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .AssociatedEntity(p => p.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 1) .LeadCoordinator.PartKey == coord.PartKey); } }
public void AssociatedEntityWhereNewValueIsNull() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); } // Simulate detach project.LeadCoordinator = null; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .AssociatedEntity(p => p.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator == null); } }
public void BaseEntityUpdate() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies.Single(p => p.Id == 2); } // Simulate detach company1.Name = "Company #1"; // Change from Company 1 to Company #1 using (var context = new TestDbContext()) { context.UpdateGraph(company1, null); context.SaveChanges(); Assert.IsTrue(context.Companies.Single(p => p.Id == 2).Name == "Company #1"); } }
public void EnsureWeCanUseCyclicRelationsOnOwnedCollections() { Models.Manager manager; using (var context = new TestDbContext()) { manager = context.Managers.Include(p => p.Employees).First(); } // Simulate disconnect var newEmployee = new Models.Employee { Key = "assdf", FirstName = "Test Employee", Manager = manager }; manager.Employees.Add(newEmployee); using (var context = new TestDbContext()) { context.UpdateGraph(manager, m1 => m1.OwnedCollection(o => o.Employees)); context.SaveChanges(); Assert.IsTrue(context.Employees.Include(p => p.Manager).Single(p => p.Key == "assdf").Manager.FirstName == manager.FirstName); } }
public void OwnedEntityUpdateValues() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); } // Simulate detach project.LeadCoordinator.FirstName = "Tada"; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .OwnedEntity(p => p.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.FirstName == "Tada"); } }
public void OwnedEntityNewEntity() { Models.Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); } // Simulate detach project.LeadCoordinator = new Models.Manager { FirstName = "Br", PartKey = "TER", PartKey2 = 2 }; using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(project, map => map .OwnedEntity(p => p.LeadCoordinator)); context.SaveChanges(); Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.PartKey == "TER"); } }
public void OwnedCollectionWithOwnedCollection() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .First(); } // Simulate detach company1.Contacts.First().Infos.First().Email = "testeremail"; company1.Contacts.First().Infos.Add(new Models.ContactInfo { Description = "Test", Email = "*****@*****.**" }); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(m => m.Infos))); context.SaveChanges(); var value = context.Companies.Include(p => p.Contacts.Select(m => m.Infos)) .First(); Assert.IsTrue(value.Contacts.First().Infos.Count == 2); Assert.IsTrue(value.Contacts.First().Infos.First().Email == "testeremail"); } }
public void OwnedCollectionRemove() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); } // Simulate detach company1.Contacts.Remove(company1.Contacts.First()); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(p => p.Infos))); context.SaveChanges(); Assert.IsTrue(context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2) .Contacts.Count == 0); } }
public void OwnedCollectionAddMultiple() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); } // Simulate detach company1.Name = "Company #1"; // Change from Company 1 to Company #1 company1.Contacts.Add(new Models.CompanyContact { FirstName = "Charlie", LastName = "Sheen", Infos = new List<Models.ContactInfo> { new Models.ContactInfo { PhoneNumber = "123456789", Description = "Home" } } }); company1.Contacts.Add(new Models.CompanyContact { FirstName = "Tim", LastName = "Sheen" }); company1.Contacts.Add(new Models.CompanyContact { FirstName = "Emily", LastName = "Sheen" }); company1.Contacts.Add(new Models.CompanyContact { FirstName = "Mr", LastName = "Sheen", Infos = new List<Models.ContactInfo> { new Models.ContactInfo { PhoneNumber = "123456789", Description = "Home" } } }); company1.Contacts.Add(new Models.CompanyContact { FirstName = "Mr", LastName = "X" }); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(p => p.Infos))); context.SaveChanges(); Assert.IsTrue(context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2) .Contacts.Count == 6); } }
public void DoesNotUpdateEntityIfNoChangesHaveBeenMade() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies.Single(p => p.Id == 2); } // Simulate detach using (var context = new TestDbContext()) { context.UpdateGraph(company1, null); Assert.IsTrue(context.ChangeTracker.Entries().All(p => p.State == System.Data.EntityState.Unchanged)); } }
public void Detached_OwnedMultipleLevelCollectionMappingWithAssociatedReload() { Models.MultiLevelTest multiLevelTest; Models.Hobby hobby; using (var context = new TestDbContext()) { multiLevelTest = context.MultiLevelTest.Add(new Models.MultiLevelTest { Managers = new[] // test arrays as well { new Models.Manager { PartKey = "xxx", PartKey2 = 2, Employees = new List <Models.Employee> { new Models.Employee { Key = "xsdf", FirstName = "Asdf", Hobbies = new List <Models.Hobby> { new Models.Hobby { HobbyType = "Test hobby type" } } } } } } }); hobby = context.Hobbies.Add(new Models.Hobby { HobbyType = "Skiing" }); context.SaveChanges(); } // Simulate detach // Graph changes // Should not update changes to hobby hobby.HobbyType = "Something Else"; // Update changes to manager var manager = multiLevelTest.Managers.First(); manager.FirstName = "Tester"; // Update changes to employees var employeeToUpdate = manager.Employees.First(); employeeToUpdate.Hobbies.Clear(); employeeToUpdate.Hobbies.Add(hobby); manager.Employees.Add(new Models.Employee { FirstName = "Tim", Key = "Tim1", Manager = multiLevelTest.Managers.First() }); using (var context = new TestDbContext()) { GraphDiffConfiguration.ReloadAssociatedEntitiesWhenAttached = true; // Setup mapping context.UpdateGraph(multiLevelTest, map => map .OwnedCollection(x => x.Managers, withx => withx .AssociatedCollection(pro => pro.Projects) .OwnedCollection(p => p.Employees, with => with .AssociatedCollection(m => m.Hobbies) .OwnedEntity(m => m.Locker)))); context.SaveChanges(); GraphDiffConfiguration.ReloadAssociatedEntitiesWhenAttached = false; var result = context.MultiLevelTest .Include("Managers.Employees.Hobbies") .Include("Managers.Employees.Locker") .Include("Managers.Projects") .First(); var updateManager = result.Managers.Single(p => p.PartKey == manager.PartKey && p.PartKey2 == manager.PartKey2); var updateEmployee = updateManager.Employees.Single(p => p.Key == employeeToUpdate.Key); var updateHobby = context.Hobbies.Single(p => p.Id == hobby.Id); Assert.IsTrue(updateManager.Employees.Count() == 2); Assert.IsTrue(result.Managers.First().FirstName == "Tester"); Assert.IsTrue(updateEmployee.Hobbies.Count() == 1); Assert.IsTrue(updateEmployee.Hobbies.First().HobbyType == "Skiing"); Assert.IsTrue(updateHobby.HobbyType == "Skiing"); Assert.IsTrue(result.Managers.First().Employees.Any(p => p.Key == "Tim1")); } }
public void OwnedCollectionAddRemoveUpdate() { Models.Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); company1.Contacts.Add(new Models.CompanyContact { FirstName = "Hello", LastName = "Test" }); context.SaveChanges(); } // Simulate detach // Update, remove and add company1.Name = "Company #1"; // Change from Company 1 to Company #1 string originalname = company1.Contacts.First().FirstName; company1.Contacts.First().FirstName = "Terrrrrry"; company1.Contacts.Remove(company1.Contacts.Skip(1).First()); company1.Contacts.Add(new Models.CompanyContact { FirstName = "Charlie", LastName = "Sheen", Infos = new List<Models.ContactInfo> { new Models.ContactInfo { PhoneNumber = "123456789", Description = "Home" } } }); using (var context = new TestDbContext()) { // Setup mapping context.UpdateGraph(company1, map => map .OwnedCollection(p => p.Contacts, with => with .OwnedCollection(p => p.Infos))); context.SaveChanges(); var test = context.Companies .Include(p => p.Contacts.Select(m => m.Infos)) .Single(p => p.Id == 2); Assert.IsTrue(test.Contacts.Count == 2); Assert.IsTrue(test.Contacts.First().FirstName == "Terrrrrry"); Assert.IsTrue(test.Contacts.Skip(1).First().FirstName == "Charlie"); } }