コード例 #1
0
        public void ShouldAddNewAggregateRoot_Attached()
        {
            var associated = new OneToOneAssociatedModel { Title = "Associated" };
            var manyAssociated = new OneToManyAssociatedModel { Title = "Associated" };
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyOwned = new List<OneToManyOwnedModel>
                {
                    new OneToManyOwnedModel { Title = "One" },
                    new OneToManyOwnedModel { Title = "Two" },
                    new OneToManyOwnedModel { Title = "Three" }
                },
                OneToManyAssociated = new List<OneToManyAssociatedModel>
                {
                    manyAssociated
                },
                OneToOneOwned = new OneToOneOwnedModel { Title = "OneToOne" },
                OneToOneAssociated = associated
            };

            using (var context = new TestDbContext())
            {
                context.OneToManyAssociatedModels.Add(manyAssociated);
                context.OneToOneAssociatedModels.Add(associated);

                     node1 = context.UpdateGraph(node1);

                context.SaveChanges();
                Assert.IsNotNull(context.Nodes.SingleOrDefault(p => p.Id == node1.Id));
            }
        }
コード例 #2
0
        public void ShouldThrowDbUpdateConcurrencyExceptionIfEditingNestedOutOfDateModel()
        {
            TestNode node;
            using (var db = new TestDbContext())
            {
                node = new TestNode
                {
                    Title = "Hello",
                    OneToManyOwned = new List<OneToManyOwnedModel>
                    {
                        new OneToManyOwnedModel { Title = "Test1" },
                        new OneToManyOwnedModel { Title = "Test2" }
                    }
                };
                db.Nodes.Add(node);
                db.SaveChanges();
            }

            using (var db = new TestDbContext())
            {
                node.OneToManyOwned.First().RowVersion = new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1 };
                db.UpdateGraph(node);
                db.SaveChanges();
            }
        }
コード例 #3
0
        public void ShouldAddRelationIfPreviousValueWasNull()
        {
            var node1 = new TestNode { Title = "New Node" };
            var associated = new OneToOneAssociatedModel { Title = "Associated Node" };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.OneToOneAssociatedModels.Add(associated);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToOneAssociated = associated;

            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, map => map
                    .AssociatedEntity(p => p.OneToOneAssociated));

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToOneAssociated).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToOneAssociated.OneParent == node2);
            }
        }
コード例 #4
0
        public void ShouldNotUpdateEntitesWithinAnAssociatedCollection()
        {
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyAssociated = new List<OneToManyAssociatedModel>
                {
                    new OneToManyAssociatedModel { Title = "First One" }
                }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToManyAssociated.First().Title = "This should not overwrite value";

            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, map => map
                    .AssociatedCollection(p => p.OneToManyAssociated));

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToManyAssociated).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToManyAssociated.Single().Title == "First One");
            }
        }
コード例 #5
0
        public void ShouldThrowDbUpdateConcurrencyExceptionWithEmptyRowVersion()
        {
            TestNode node;
            using (var db = new TestDbContext())
            {
                node = new TestNode
                {
                    Title = "Hello",
                    OneToManyOwned = new List<OneToManyOwnedModel>
                    {
                        new OneToManyOwnedModel { Title = "Test1" },
                        new OneToManyOwnedModel { Title = "Test2" }
                    }
                };
                db.Nodes.Add(node);
                db.SaveChanges();
            }

            using (var db = new TestDbContext())
            {
                node.OneToManyOwned.First().RowVersion = null;
                db.UpdateGraph(node, map => map.OwnedCollection(p => p.OneToManyOwned));
                db.SaveChanges();
            }
        }
コード例 #6
0
        public void ShouldBeAbleToVisitExpressionsStoredAsProperties()
        {
            var node1 = new TestNode
            {
                Title = "One",
                OneToOneOwned = new OneToOneOwnedModel { Title = "Hello" }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToOneOwned.Title = "Hey2";

            Lambda = (p => p.OneToOneOwned);
            Expression<Func<IUpdateConfiguration<TestNode>, dynamic>> exp = map => map.OwnedEntity(Lambda);

            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, exp);

                context.SaveChanges();
                Assert.IsTrue(context.Nodes
                    .Include(p => p.OneToOneOwned)
                    .Single(p => p.Id == node1.Id)
                    .OneToOneOwned.Title == "Hey2");
            }
        }
コード例 #7
0
        public void ShouldAddNewItemInOwnedCollection()
        {
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyOwned = new List<OneToManyOwnedModel>
                {
                    new OneToManyOwnedModel { Title = "Hello" }
                }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            var newModel = new OneToManyOwnedModel { Title = "Hi" };
            node1.OneToManyOwned.Add(newModel);
            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, map => map
                    .OwnedCollection(p => p.OneToManyOwned));

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToManyOwned.Count == 2);
                var owned = context.OneToManyOwnedModels.Single(p => p.Id == newModel.Id);
                Assert.IsTrue(owned.OneParent == node2 && owned.Title == "Hi");
            }
        }
コード例 #8
0
        public void ShouldNotUpdatePropertiesOfAnAssociatedEntity()
        {
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToOneAssociated = new OneToOneAssociatedModel { Title = "Associated Node" }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToOneAssociated.Title = "Updated Content";

            using (var context = new TestDbContext())
            {
                context.UpdateGraph(node1);

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToOneAssociated).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToOneAssociated.OneParent == node2);
                // should not delete it as it is associated and no cascade rule set.
                Assert.IsTrue(node2.OneToOneAssociated.Title == "Associated Node");
            }
        }
コード例 #9
0
        public void ShouldSupportNullValuesInTree()
        {
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToOneOwned = null
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            using (var context = new TestDbContext())
            {
                // Setup mapping
                node1 = context.UpdateGraph(node1, map => map
                    .OwnedEntity(p => p.OneToOneOwned, with =>
                        with.OwnedEntity(p => p.OneToOneOneToOneOwned)));

                context.SaveChanges();
                context.Entry(node1).Reload();
                Assert.IsTrue(node1.OneToOneOwned == null);
            }
        }
コード例 #10
0
        public void ShouldAddRelationToExistingAssociatedCollection()
        {
            var associated = new OneToManyAssociatedModel { Title = "Second One" };
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyAssociated = new List<OneToManyAssociatedModel>
                {
                    new OneToManyAssociatedModel { Title = "First One" }
                }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.OneToManyAssociatedModels.Add(associated);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToManyAssociated.Add(associated);

            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, map => map
                    .AssociatedCollection(p => p.OneToManyAssociated));

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToManyAssociated).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToManyAssociated.Count == 2);
            }
        }
コード例 #11
0
        public void ShouldUpdateItemInNestedOwnedCollection()
        {
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyOwned = new List<OneToManyOwnedModel>
                {
                    new OneToManyOwnedModel
                    {
                        Title = "Hello",
                        OneToManyOneToManyOwned = new Collection<OneToManyOneToManyOwnedModel>
                        {
                            new OneToManyOneToManyOwnedModel {Title = "BeforeUpdate"}
                        }
                    }
                }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            var oneToManyOneToManyOwned = node1.OneToManyOwned.Single().OneToManyOneToManyOwned.Single();
            var expectedId = oneToManyOneToManyOwned.Id;
            const string expectedTitle = "AfterUpdate";
            oneToManyOneToManyOwned.Title = expectedTitle;

            using (var context = new TestDbContext())
            {
                // Setup mapping
                node1 = context.UpdateGraph(node1, map => map.OwnedCollection(p => p.OneToManyOwned,
                                                                              with => with.OwnedCollection(p => p.OneToManyOneToManyOwned)));
                context.SaveChanges();

                Assert.AreEqual(1, node1.OneToManyOwned.Count);
                Assert.AreEqual(1, node1.OneToManyOwned.Single().OneToManyOneToManyOwned.Count);

                oneToManyOneToManyOwned = node1.OneToManyOwned.Single().OneToManyOneToManyOwned.Single();
                Assert.AreEqual(expectedId, oneToManyOneToManyOwned.Id);
                Assert.AreEqual(expectedTitle, oneToManyOneToManyOwned.Title);

                var node1Reloaded = context.Nodes
                        .Include("OneToManyOwned.OneToManyOneToManyOwned")
                        .Single(n => n.Id == node1.Id);

                Assert.AreEqual(1, node1Reloaded.OneToManyOwned.Count);
                Assert.AreEqual(1, node1Reloaded.OneToManyOwned.Single().OneToManyOneToManyOwned.Count);

                oneToManyOneToManyOwned = node1Reloaded.OneToManyOwned.Single().OneToManyOneToManyOwned.Single();
                Assert.AreEqual(expectedId, oneToManyOneToManyOwned.Id);
                Assert.AreEqual(expectedTitle, oneToManyOneToManyOwned.Title);
            }
        }
コード例 #12
0
ファイル: AttachedBehaviours.cs プロジェクト: rho24/GraphDiff
 public void ShouldThrowExceptionIfAggregateIsNotDetached()
 {
     using (var context = new TestDbContext())
     {
         var node = new TestNode();
         context.Nodes.Add(node);
         node.Title = "Hello";
         context.UpdateGraph(node);
         context.SaveChanges();
     }
 }
コード例 #13
0
        public void ShouldAddSingleEntity()
        {
            var node1 = new TestNode
            {
                Title = "Hello"
            };

            using (var context = new TestDbContext())
            {
                node1 = context.UpdateGraph(node1);
                context.SaveChanges();
                Assert.IsNotNull(context.Nodes.SingleOrDefault(p => p.Id == node1.Id));
            }
        }
コード例 #14
0
        public void ShouldNotUpdateEntityIfNoChangesHaveBeenMade_Detached()
        {
            var node1 = new TestNode
            {
                Title = "Hello"
            };

            using (var context = new TestDbContext())
            {
                node1 = context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            using (var context = new TestDbContext())
            {
                context.UpdateGraph(node1);
                Assert.IsTrue(context.ChangeTracker.Entries().All(p => p.State == EntityState.Unchanged));
                context.SaveChanges();
            }
        }
コード例 #15
0
        public void ShouldUpdateValuesOfEntityWhenEntityAlreadyExists()
        {
            var node1 = new TestNode { Title = "New Node", OneToOneOwned = new OneToOneOwnedModel { Title = "New Entity" } };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToOneOwned.Title = "Newer Entity";
            using (var context = new TestDbContext())
            {
                context.UpdateGraph(node1);

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToOneOwned).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToOneOwned.OneParent == node2 && node2.OneToOneOwned.Title == "Newer Entity");
            }
        }
コード例 #16
0
        public void ShouldMergeTwoCollectionsAndDecideOnUpdatesDeletesAndAdds()
        {
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyOwned = new List<OneToManyOwnedModel>
                {
                    new OneToManyOwnedModel { Title = "This" },
                    new OneToManyOwnedModel { Title = "Is" },
                    new OneToManyOwnedModel { Title = "A" },
                    new OneToManyOwnedModel { Title = "Test" }
                }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToManyOwned.Remove(node1.OneToManyOwned.First());
            node1.OneToManyOwned.First().Title = "Hello";
            node1.OneToManyOwned.Add(new OneToManyOwnedModel { Title = "Finish" });
            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, map => map
                    .OwnedCollection(p => p.OneToManyOwned));

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                var list = node2.OneToManyOwned.ToList();
                Assert.IsTrue(list[0].Title == "Hello");
                Assert.IsTrue(list[1].Title == "A");
                Assert.IsTrue(list[2].Title == "Test");
                Assert.IsTrue(list[3].Title == "Finish");
            }
        }
コード例 #17
0
        public void ShouldNotRemoveEntityIfRemovedFromParent()
        {
            var oneToOne = new OneToOneOwnedModel { Title = "New Entity" };
            var node1 = new TestNode { Title = "New Node", OneToOneOwned = oneToOne };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToOneOwned = null;
            using (var context = new TestDbContext())
            {
                context.UpdateGraph(node1);

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToOneOwned).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                     Assert.IsNotNull(context.OneToOneOwnedModels.SingleOrDefault(p => p.Id == oneToOne.Id));
            }
        }
コード例 #18
0
        public void ShouldUpdateSingleEntity_Detached()
        {
            var node1 = new TestNode
            {
                Title = "Hello"
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.Title = "Hello2";

            using (var context = new TestDbContext())
            {
                context.UpdateGraph(node1);
                context.SaveChanges();
                Assert.IsTrue(context.Nodes.Single(p => p.Id == node1.Id).Title == "Hello2");
            }
        }
コード例 #19
0
        public void ShouldAddNewEntityWhenAddedToParent()
        {
            var node1 = new TestNode { Title = "New Node" };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToOneOwned = new OneToOneOwnedModel { Title = "New Entity" };
            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, map => map
                    .OwnedEntity(p => p.OneToOneOwned));

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToOneOwned).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToOneOwned.OneParent == node2 && node2.OneToOneOwned.Title == "New Entity");
            }
        }
コード例 #20
0
        public void ShouldNotRemoveAssociatedRelationIfNull()
        {
            var node1 = new TestNode { Title = "New Node", OneToOneAssociated = new OneToOneAssociatedModel { Title = "Associated Node" } };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToOneAssociated = null;

            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1);

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToOneAssociated).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsFalse(node2.OneToOneAssociated == null);
            }
        }
コード例 #21
0
        public void ShouldThrowDbUpdateConcurrencyExceptionIfEditingOutOfDateModel()
        {
            TestNode node;
            using (var db = new TestDbContext())
            {
                node = new TestNode { Title = "Hello" };
                db.Nodes.Add(node);
                db.SaveChanges();
            }

            using (var db = new TestDbContext())
            {
                var node2 = new TestNode
                {
                    RowVersion = new byte[] { 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1 },
                    Id = node.Id,
                    Title = "Test2"
                };

                db.UpdateGraph(node2);
                db.SaveChanges();
            }
        }
コード例 #22
0
        public void ShouldRemoveItemsInOwnedCollection()
        {
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyOwned = new List<OneToManyOwnedModel>
                {
                    new OneToManyOwnedModel { Title = "Hello" },
                    new OneToManyOwnedModel { Title = "Hello2" },
                    new OneToManyOwnedModel { Title = "Hello3" }
                }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToManyOwned.Remove(node1.OneToManyOwned.First());
            node1.OneToManyOwned.Remove(node1.OneToManyOwned.First());
            node1.OneToManyOwned.Remove(node1.OneToManyOwned.First());
            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, map => map
                    .OwnedCollection(p => p.OneToManyOwned));

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToManyOwned.Count == 0);
            }
        }
コード例 #23
0
        public void ShouldSupportOwnedCollectionWithNestedModels()
        {
            // setup
            var oneToOneAssociations = new List<OneToManyOneToOneAssociatedModel>();
            var oneToManyAssociations = new List<OneToManyOneToManyAssociatedModel>();

            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyOwned = new List<OneToManyOwnedModel>()
            };

            for (var i = 0; i < 3; i++)
            {
                node1.OneToManyOwned.Add(new OneToManyOwnedModel
                {
                    OneToManyOneToOneAssociated = new OneToManyOneToOneAssociatedModel { Title = "Hello" },
                    OneToManyOneToOneOwned = new OneToManyOneToOneOwnedModel { Title = "Hello" },
                    OneToManyOneToManyAssociated = new List<OneToManyOneToManyAssociatedModel>
                        {
                            new OneToManyOneToManyAssociatedModel { Title = "Hello" },
                            new OneToManyOneToManyAssociatedModel { Title = "Hello" }
                        },
                    OneToManyOneToManyOwned = new List<OneToManyOneToManyOwnedModel>
                        {
                            new OneToManyOneToManyOwnedModel { Title = "Hello" },
                            new OneToManyOneToManyOwnedModel { Title = "Hello" },
                            new OneToManyOneToManyOwnedModel { Title = "Hello" }
                        }
                });

                oneToOneAssociations.Add(new OneToManyOneToOneAssociatedModel { Title = "Associated Update" });
                oneToManyAssociations.Add(new OneToManyOneToManyAssociatedModel { Title = "Many Associated Update" + i });
            }

            using (var context = new TestDbContext())
            {
                foreach (var association in oneToOneAssociations)
                {
                    context.Set<OneToManyOneToOneAssociatedModel>().Add(association);
                }
                foreach (var association in oneToManyAssociations)
                {
                    context.Set<OneToManyOneToManyAssociatedModel>().Add(association);
                }
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            // change
            var collection = node1.OneToManyOwned;
            for (var i = 0; i < collection.Count; i++)
            {
                var element = collection.Skip(i).First();
                element.OneToManyOneToOneOwned.Title = "Updated" + i;
                element.OneToManyOneToOneAssociated = oneToOneAssociations[i];

                var owned = element.OneToManyOneToManyOwned;
                owned.Remove(owned.First());
                owned.First().Title = "Updated" + i;
                owned.Skip(1).First().Title = "Updated 2" + i;
                owned.Add(new OneToManyOneToManyOwnedModel { Title = "A new one" + i });

                var associated = element.OneToManyOneToManyAssociated;
                associated.Remove(associated.First());
                associated.Add(oneToManyAssociations[i]);
            }

            using (var context = new TestDbContext())
            {
                context.UpdateGraph(node1);

                context.SaveChanges();

                // assert
                node1 = context.Nodes.Single();
                var list = node1.OneToManyOwned.ToList();
                for (var i = 0; i < collection.Count; i++)
                {
                    var element = list[i];
                    Assert.IsNotNull(element);
                    Assert.IsTrue(element.OneToManyOneToOneOwned.Title == "Updated" + i);
                    Assert.IsTrue(element.OneToManyOneToOneAssociated.Title == "Associated Update");

                    var ownershipList = element.OneToManyOneToManyOwned.ToList();
                    Assert.IsTrue(ownershipList.Count == 3);
                    Assert.IsTrue(ownershipList[0].Title == "Updated" + i);
                    Assert.IsTrue(ownershipList[1].Title == "Updated 2" + i);
                    Assert.IsTrue(ownershipList[2].Title == "A new one" + i);

                    var associatedList = element.OneToManyOneToManyAssociated.ToList();
                    Assert.IsTrue(associatedList.Count == 2);
                    Assert.IsTrue(associatedList[0].Title == "Hello");
                    Assert.IsTrue(associatedList[1].Title == "Many Associated Update" + i);
                }
            }
        }
コード例 #24
0
        public void ShouldUpdateAggregateWithOwnedEntityAndOwnedCollection()
        {
            var node1 = new TestNode
            {
                Title = "New Node"
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            }

            using (var context = new TestDbContext())
            {
                node1.Title = "Newly Updated";
                node1.OneToOneOwned = new OneToOneOwnedModel
                {
                    OneToOneOneToManyOwned = new[]
                    {
                        new OneToOneOneToManyOwnedModel {Title = "One"},
                        new OneToOneOneToManyOwnedModel {Title = "Two"},
                        new OneToOneOneToManyOwnedModel {Title = "Three"}
                    }
                };

                node1 = context.UpdateGraph(node1);
                context.SaveChanges();
            }

            using (var context = new TestDbContext())
            {
                var reload = context.Nodes
                    .Include("OneToOneOwned.OneToOneOneToManyOwned")
                    .SingleOrDefault(p => p.Id == node1.Id);

                Assert.IsNotNull(reload);
                Assert.AreEqual(node1.Title, reload.Title);
                Assert.IsNotNull(reload.OneToOneOwned);
                Assert.AreEqual(node1.OneToOneOwned.Id, reload.OneToOneOwned.Id);

                Assert.IsNotNull(reload.OneToOneOwned.OneToOneOneToManyOwned);
                Assert.AreEqual(3, reload.OneToOneOwned.OneToOneOneToManyOwned.Count);
                Assert.AreEqual(node1.OneToOneOwned.OneToOneOneToManyOwned.First().Id, node1.OneToOneOwned.OneToOneOneToManyOwned.First().Id);

            }
        }
コード例 #25
0
        public void ShouldSupportOwnedEntityWithNestedModels()
        {
            // setup
            var oneToOneAssociated = new OneToOneOneToOneAssociatedModel { Title = "Associated Update" };
            var oneToManyAssociated = new OneToOneOneToManyAssociatedModel { Title = "Many Associated Update" };
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToOneOwned = new OneToOneOwnedModel
                {
                    OneToOneOneToOneAssociated = new OneToOneOneToOneAssociatedModel { Title = "Hello" },
                    OneToOneOneToOneOwned = new OneToOneOneToOneOwnedModel { Title = "Hello" },
                    OneToOneOneToManyAssociated = new List<OneToOneOneToManyAssociatedModel>
                    {
                        new OneToOneOneToManyAssociatedModel { Title = "Hello" },
                        new OneToOneOneToManyAssociatedModel { Title = "Hello" }
                    },
                    OneToOneOneToManyOwned = new List<OneToOneOneToManyOwnedModel>
                    {
                        new OneToOneOneToManyOwnedModel { Title = "Hello" },
                        new OneToOneOneToManyOwnedModel { Title = "Hello" },
                        new OneToOneOneToManyOwnedModel { Title = "Hello" }
                    }
                }
            };

            using (var context = new TestDbContext())
            {
                context.Set<OneToOneOneToOneAssociatedModel>().Add(oneToOneAssociated);
                context.Set<OneToOneOneToManyAssociatedModel>().Add(oneToManyAssociated);
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            // change
            node1.OneToOneOwned.OneToOneOneToOneOwned.Title = "Updated";
            node1.OneToOneOwned.OneToOneOneToOneAssociated = oneToOneAssociated;

            var owned = node1.OneToOneOwned.OneToOneOneToManyOwned;
            owned.Remove(owned.First());
            owned.First().Title = "Updated";
            owned.Skip(1).First().Title = "Updated 2";
            owned.Add(new OneToOneOneToManyOwnedModel { Title = "A new one" });

            var associated = node1.OneToOneOwned.OneToOneOneToManyAssociated;
            associated.Remove(associated.First());
            associated.Add(oneToManyAssociated);

            using (var context = new TestDbContext())
            {
                context.UpdateGraph(node1);

                context.SaveChanges();

                var updated = context.Set<OneToOneOwnedModel>().Single();
                Assert.IsNotNull(updated);
                Assert.IsTrue(updated.OneToOneOneToOneOwned.Title == "Updated");
                Assert.IsTrue(updated.OneToOneOneToOneAssociated.Title == "Associated Update");

                var ownershipList = updated.OneToOneOneToManyOwned.ToList();
                Assert.IsTrue(ownershipList.Count == 3);
                Assert.IsTrue(ownershipList[0].Title == "Updated");
                Assert.IsTrue(ownershipList[1].Title == "Updated 2");
                Assert.IsTrue(ownershipList[2].Title == "A new one");

                var associatedList = updated.OneToOneOneToManyAssociated.ToList();
                Assert.IsTrue(associatedList.Count == 2);
                Assert.IsTrue(associatedList[0].Title == "Hello");
                Assert.IsTrue(associatedList[1].Title == "Many Associated Update");
            }
        }
コード例 #26
0
        public void ShouldUpdateItemInOwnedCollection()
        {
            var node1 = new TestNode
            {
                Title = "New Node",
                OneToManyOwned = new List<OneToManyOwnedModel>
                {
                    new OneToManyOwnedModel { Title = "Hello" }
                }
            };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToManyOwned.First().Title = "What's up";
            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1);

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToManyOwned).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                var owned = node2.OneToManyOwned.First();
                Assert.IsTrue(owned.OneParent == node2 && owned.Title == "What's up");
            }
        }
コード例 #27
0
        public void ShouldReplaceReferenceIfNewEntityIsNotPreviousEntity()
        {
            var node1 = new TestNode { 
                Title = "New Node",
                OneToOneAssociated = new OneToOneAssociatedModel { Title = "Associated Node" }
            };
            var otherModel = new OneToOneAssociatedModel { Title = "Hello" };

            using (var context = new TestDbContext())
            {
                context.Nodes.Add(node1);
                context.OneToOneAssociatedModels.Add(otherModel);
                context.SaveChanges();
            } // Simulate detach

            node1.OneToOneAssociated = otherModel;

            using (var context = new TestDbContext())
            {
                // Setup mapping
                context.UpdateGraph(node1, map => map
                    .AssociatedEntity(p => p.OneToOneAssociated));

                context.SaveChanges();
                var node2 = context.Nodes.Include(p => p.OneToOneAssociated).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToOneAssociated.OneParent == node2);
                // should not delete it as it is associated and no cascade rule set.
                Assert.IsTrue(context.OneToOneAssociatedModels.Single(p => p.Id != otherModel.Id).OneParent == null);
            }
        }