Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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.
            }
        }
Esempio n. 3
0
        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");
            }
        }
Esempio n. 4
0
        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");
            }
        }
Esempio n. 5
0
        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);
            }
        }
Esempio n. 6
0
        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);
            }
        }
Esempio n. 7
0
        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);
            }
        }
Esempio n. 8
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));
            }
        }
Esempio n. 9
0
        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");
            }
        }
Esempio n. 10
0
        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");
            }
        }
Esempio n. 11
0
        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");
            }
        }
Esempio n. 12
0
        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);
            }
        }
Esempio n. 13
0
        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);
            }
        }
Esempio n. 14
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");
            }
        }
Esempio n. 15
0
        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");
            }
        }
Esempio n. 16
0
        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");
            }
        }
Esempio n. 17
0
        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));
            }
        }
Esempio n. 18
0
        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");
            }
        }
Esempio n. 19
0
        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);
            }
        }
Esempio n. 20
0
        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);
            }
        }
Esempio n. 21
0
        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"));
            }
        }
Esempio n. 22
0
        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");
            }
        }
Esempio n. 23
0
        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"));
            }
        }
Esempio n. 24
0
        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);
            }
        }
Esempio n. 25
0
        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));
            }
        }
Esempio n. 26
0
        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);
            }
        }
Esempio n. 27
0
        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");
            }
        }
Esempio n. 28
0
        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");
            }
        }
Esempio n. 29
0
        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);
            }
        }
Esempio n. 30
0
        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);
            }
        }
Esempio n. 31
0
        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");
            }
        }
Esempio n. 32
0
        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);
            }
        }
Esempio n. 33
0
        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");
            }
        }
Esempio n. 34
0
        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");
            }
        }
Esempio n. 35
0
        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");
            }
        }
Esempio n. 36
0
        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");
            }
        }
Esempio n. 37
0
        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);
            }
        }
Esempio n. 38
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);
            }
        }
Esempio n. 39
0
        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));
            }
        }
Esempio n. 40
0
        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"));
            }
        }
Esempio n. 41
0
        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");
            }
        }