Exemplo n.º 1
0
        public void Detached_AssociatedEntityValuesOnInsert()
        {
            // recupera um Coordernador
            Models.Manager coord;
            using (var context = new TestDbContext(_connection))
            {
                coord = context.Managers
                    .Single(p => p.PartKey == "manager1" && p.PartKey2 == 1);

            } // Simulate detach

            // Create project
            var project = new Project()
            {
                Name = "OnInsert",
                LeadCoordinator = coord,
            };

            // Tenta alterar o coordenador.
            coord.FirstName = "Larry";

            using (var context = new TestDbContext(_connection))
            {
                // 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(_connection))
            {
                var proj = context.Projects
                    .Include(p => p.LeadCoordinator)
                    .SingleOrDefault(p => p.Name == project.Name);

                Assert.IsNotNull(proj);
                Assert.IsTrue(proj.LeadCoordinator.FirstName == "Trent");
            }
        }
Exemplo n.º 2
0
        public void Detached_AssociatedCollectionOnInsert()
        {
            // don't know what to do about this yet..
            Models.Company company2;
            using (var context = new TestDbContext(_connection))
            {
                company2 = context.Companies.Single(p => p.Id == 2);
            } // Simulate detach

            var project = new Project()
            {
                Name = "Another Project",
                Deadline = DateTime.Now,
                Stakeholders = new List<Company>() { company2 },
            };

            using (var context = new TestDbContext(_connection))
            {
                // Setup mapping
                context.UpdateGraph(project, map => map.AssociatedCollection(p => p.Stakeholders));
                context.SaveChanges();
            }

            using (var context = new TestDbContext(_connection))
            {
                var projCadastrado = context.Projects
                    .Include(p => p.Stakeholders)
                    .Single(p => p.Name == "Another Project");

                Assert.IsNotNull(projCadastrado);
                Assert.AreEqual(1, projCadastrado.Stakeholders.Count);
            }
        }