public void AssociatedCollectionAddById3() { // don't know what to do about this yet.. Project project1; using (var context = new TestDbContext()) { project1 = context.Projects .Include(p => p.Stakeholders) // can be removed .Single(p => p.Id == 2); } // Simulate detach project1.Stakeholders = new List<Company>() { new Company {Id = 3} }; 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 == 1); } }
public void AssociatedCollectionAdd() { // don't know what to do about this yet.. Project project1; Company company2; int count; 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); count = project1.Stakeholders.Count(); } // 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 == count + 1); } }
public void OwnedEntityUpdateValues() { 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() { Project project; using (var context = new TestDbContext()) { project = context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2); } // Simulate detach project.LeadCoordinator = new 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() { 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 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() { Company company1; using (var context = new TestDbContext()) { company1 = context.Companies .Include(p => p.Contacts) .Single(p => p.Id == 1); } // Simulate detach company1.Name = "Company #1"; // Change from Company 1 to Company #1 company1.Contacts.First().FirstName = "Bobby"; // change from bob 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 == 1) .Contacts.First() .FirstName == "Bobby"); Assert.IsTrue(context.Companies .Include(p => p.Contacts) .Single(p => p.Id == 1) .Contacts.First() .LastName == "Brown"); } }
public void OwnedCollectionRemove() { 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 OwnedCollectionAddRemoveUpdate() { 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 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 CompanyContact { FirstName = "Charlie", LastName = "Sheen", Infos = new List<ContactInfo> { new 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 OwnedCollectionAddMultiple() { 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 CompanyContact { FirstName = "Charlie", LastName = "Sheen", Infos = new List<ContactInfo> { new ContactInfo { PhoneNumber = "123456789", Description = "Home" } } }); company1.Contacts.Add(new CompanyContact { FirstName = "Tim", LastName = "Sheen" }); company1.Contacts.Add(new CompanyContact { FirstName = "Emily", LastName = "Sheen" }); company1.Contacts.Add(new CompanyContact { FirstName = "Mr", LastName = "Sheen", Infos = new List<ContactInfo> { new ContactInfo { PhoneNumber = "123456789", Description = "Home" } } }); company1.Contacts.Add(new 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 BaseEntityUpdate() { 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 AssociatedEntityWherePreviousValueWasNull() { Project project; 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() { 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 AssociatedEntityValuesShouldNotBeUpdated() { 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 AssociatedEntityValuesForNewValueShouldNotBeUpdated() { Project project; 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(); Assert.IsTrue(context.Projects .Include(p => p.LeadCoordinator) .Single(p => p.Id == 2) .LeadCoordinator.FirstName == "Trent"); } }
public void AssociatedCollectionsEntitiesValuesShouldNotBeUpdated() { 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 AssociatedCollectionRemove() { 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)); } }