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();
                TestNode node2 = context.Nodes.Include(p => p.OneToOneAssociated).Single(p => p.Id == node1.Id);
                Assert.IsNotNull(node2);
                Assert.IsTrue(node2.OneToOneAssociated.OneParent == node2);
            }
        }
        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);
            }
        }
        public void ShouldAddNewAggregateRoot_Detached()
        {
            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);
                context.SaveChanges();
            } // Simulate detach

            using (var context = new TestDbContext())
            {
                // Setup mapping
                node1 = context.UpdateGraph(node1, map => map
                                            .OwnedEntity(p => p.OneToOneOwned)
                                            .AssociatedEntity(p => p.OneToOneAssociated)
                                            .OwnedCollection(p => p.OneToManyOwned, with => with.OwnedEntity(p => p.OneToManyOneToOneOwned))
                                            .AssociatedCollection(p => p.OneToManyAssociated));

                context.SaveChanges();
                Assert.IsNotNull(context.Nodes.SingleOrDefault(p => p.Id == node1.Id));
            }
        }