Beispiel #1
0
 public static void Initialize(TestContext context)
 {
     Database.SetInitializer(new DropCreateDatabaseAlways<TestDbContext>());
     using (var db = new TestDbContext())
     {
         if (db.Database.Exists())
         {
             db.Database.Delete();
         }
         db.Database.Create();
     }
 }
Beispiel #2
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);
            }
        }
Beispiel #3
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);
            }
        }
Beispiel #4
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));
            }
        }
Beispiel #5
0
        public static void SetupTheDatabase(TestContext testContext)
        {
            using (var context = new TestDbContext())
            {
                var company1 = context.Companies.Add(new Models.Company
                {
                    Name     = "Company 1",
                    Contacts = new List <Models.CompanyContact>
                    {
                        new Models.CompanyContact
                        {
                            FirstName = "Bob",
                            LastName  = "Brown",
                            Infos     = new List <Models.ContactInfo>
                            {
                                new Models.ContactInfo
                                {
                                    Description = "Home",
                                    Email       = "*****@*****.**",
                                    PhoneNumber = "0255525255"
                                }
                            }
                        }
                    }
                });

                var company2 = context.Companies.Add(new Models.Company
                {
                    Name     = "Company 2",
                    Contacts = new List <Models.CompanyContact>
                    {
                        new Models.CompanyContact
                        {
                            FirstName = "Tim",
                            LastName  = "Jones",
                            Infos     = new List <Models.ContactInfo>
                            {
                                new Models.ContactInfo
                                {
                                    Description = "Work",
                                    Email       = "*****@*****.**",
                                    PhoneNumber = "456456456456"
                                }
                            }
                        }
                    }
                });

                var project1 = context.Projects.Add(new Models.Project
                {
                    Name         = "Major Project 1",
                    Deadline     = DateTime.Now,
                    Stakeholders = new List <Models.Company> {
                        company2
                    }
                });

                var project2 = context.Projects.Add(new Models.Project
                {
                    Name         = "Major Project 2",
                    Deadline     = DateTime.Now,
                    Stakeholders = new List <Models.Company> {
                        company1
                    }
                });

                var manager1 = context.Managers.Add(new Models.Manager
                {
                    PartKey   = "manager1",
                    PartKey2  = 1,
                    FirstName = "Trent"
                });
                var manager2 = context.Managers.Add(new Models.Manager
                {
                    PartKey   = "manager2",
                    PartKey2  = 2,
                    FirstName = "Timothy"
                });

                var locker1 = new Models.Locker
                {
                    Combination = "Asdfasdf",
                    Location    = "Middle Earth"
                };

                var employee = new Models.Employee
                {
                    Manager   = manager1,
                    Key       = "Asdf",
                    FirstName = "Test employee",
                    Locker    = locker1
                };

                context.Lockers.Add(locker1);
                context.Employees.Add(employee);

                project2.LeadCoordinator = manager2;

                context.SaveChanges();
            }
        }
Beispiel #6
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");
            }
        }
Beispiel #7
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");
            }
        }
Beispiel #8
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);
            }
        }
Beispiel #9
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");
            }
        }
Beispiel #10
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");
            }
        }
Beispiel #11
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));
            }
        }
Beispiel #12
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);
            }
        }
Beispiel #13
0
        public void Attached_OwnedMultipleLevelCollectionMappingWithAssociatedReload()
        {
            var hoby = new Hobby()
            {
                HobbyType = "Teste Hoby"
            };


            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   = Enumerable.Range(1, 2000).Select(s => hoby).ToList()
                                }
                            }
                        },
                        new Models.Manager
                        {
                            PartKey   = "xxx7",
                            PartKey2  = 3,
                            Employees = new List <Models.Employee>
                            {
                                new Models.Employee
                                {
                                    Key       = "xs7df",
                                    FirstName = "Asdf",
                                    Hobbies   = Enumerable.Range(1, 3000).Select(s => hoby).ToList()
                                }
                            }
                        }
                    }
                });

                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",
                    Hobbies   = Enumerable.Range(1, 2000).Select(s => hoby).ToList(),
                    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"));
            }
        }
Beispiel #14
0
        public void Attached_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);
            }
        }
Beispiel #15
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);
            }
        }
Beispiel #16
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");
            }
        }
Beispiel #17
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);
            }
        }
Beispiel #18
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");
            }
        }
Beispiel #19
0
        public static void SetupTheDatabase(TestContext testContext)
        {
            using (var context = new TestDbContext())
            {
                var company1 = context.Companies.Add(new Models.Company
                {
                    Name = "Company 1",
                    Contacts = new List<Models.CompanyContact>
                    {
                        new Models.CompanyContact
                        {
                            FirstName = "Bob",
                            LastName = "Brown",
                            Infos = new List<Models.ContactInfo>
                            {
                                new Models.ContactInfo
                                {
                                    Description = "Home",
                                    Email = "*****@*****.**",
                                    PhoneNumber = "0255525255"
                                }
                            }
                        }
                    }
                });

                var company2 = context.Companies.Add(new Models.Company
                {
                    Name = "Company 2",
                    Contacts = new List<Models.CompanyContact>
                    {
                        new Models.CompanyContact
                        {
                            FirstName = "Tim",
                            LastName = "Jones",
                            Infos = new List<Models.ContactInfo>
                            {
                                new Models.ContactInfo
                                {
                                    Description = "Work",
                                    Email = "*****@*****.**",
                                    PhoneNumber = "456456456456"
                                }
                            }
                        }
                    }
                });

                var project1 = context.Projects.Add(new Models.Project
                {
                    Name = "Major Project 1",
                    Deadline = DateTime.Now,
                    Stakeholders = new List<Models.Company> { company2 }
                });

                var project2 = context.Projects.Add(new Models.Project
                {
                    Name = "Major Project 2",
                    Deadline = DateTime.Now,
                    Stakeholders = new List<Models.Company> { company1 }
                });

                var manager1 = context.Managers.Add(new Models.Manager
                {
                    PartKey = "manager1",
                    PartKey2 = 1,
                    FirstName = "Trent"
                });
                var manager2 = context.Managers.Add(new Models.Manager
                {
                    PartKey = "manager2",
                    PartKey2 = 2,
                    FirstName = "Timothy"
                });

                var locker1 = new Models.Locker
                {
                    Combination = "Asdfasdf",
                    Location = "Middle Earth"
                };

                var employee = new Models.Employee
                {
                    Manager = manager1,
                    Key = "Asdf",
                    FirstName = "Test employee",
                    Locker = locker1
                };

                context.Lockers.Add(locker1);
                context.Employees.Add(employee);

                project2.LeadCoordinator = manager2;

                context.SaveChanges();
            }
        }
Beispiel #20
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");
            }
        }
Beispiel #21
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);
            }
        }
Beispiel #22
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"));
            }
        }
Beispiel #23
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");
            }
        }