public void ShouldAddAssociatedWithoutChangingRequiredAssociate()
        {
            var root = new RootEntity { RequiredAssociate = new RequiredAssociate(), Sources = new List<RootEntity>() };
            var requiredAssociate = new RequiredAssociate();
            using (var context = new TestDbContext())
            {
                context.RootEntities.Add(root);
                context.RequiredAssociates.Add(requiredAssociate);
                context.SaveChanges();
            } // Simulate detach

            var expectedAssociateId = requiredAssociate.Id;
            var owned = new RootEntity { RequiredAssociate = requiredAssociate };
            root.Sources.Add(owned);

            using (var context = new TestDbContext())
            {
                root = context.UpdateGraph(root, map => map.AssociatedCollection(r => r.Sources));
                context.SaveChanges();

                var ownedAfterSave = root.Sources.FirstOrDefault();
                Assert.IsNotNull(ownedAfterSave);
                Assert.IsNotNull(ownedAfterSave.RequiredAssociate);
                Assert.AreEqual(expectedAssociateId, ownedAfterSave.RequiredAssociate.Id);

                var ownedReloaded = context.RootEntities.Single(r => r.Id == ownedAfterSave.Id);
                Assert.IsNotNull(ownedReloaded.RequiredAssociate);
                Assert.AreEqual(expectedAssociateId, ownedReloaded.RequiredAssociate.Id);
            }
        }
        public void ShouldAddAssociateWithRequiredAssociate()
        {
            var targetRequired = new RequiredAssociate();
            var source = new RootEntity();
            var target = new RootEntity();

            using (var context = new TestDbContext())
            {
                context.RequiredAssociates.Add(targetRequired);

                context.RootEntities.Add(source);
                source.RequiredAssociate = targetRequired;

                context.RootEntities.Add(target);
                target.RequiredAssociate = targetRequired;

                context.SaveChanges();
            }

            target.Sources = new List<RootEntity> { source };

            int expectedSourceId = source.Id;
            int expectedTargetId = target.Id;
            int expectedTargetRequiredId = targetRequired.Id;
            using (var context = new TestDbContext())
            {
                context.UpdateGraph(target, map => map.AssociatedEntity(c => c.RequiredAssociate).AssociatedCollection(c => c.Sources));
                context.SaveChanges();

                Assert.IsNotNull(context.RequiredAssociates.FirstOrDefault(p => p.Id == expectedTargetRequiredId));
                Assert.IsNotNull(context.RootEntities.FirstOrDefault(p => p.Id == expectedSourceId));

                var targetReloaded = context.RootEntities.Include("Sources").FirstOrDefault(c => c.Id == expectedTargetId);
                Assert.IsNotNull(targetReloaded);
                Assert.AreEqual(1, targetReloaded.Sources.Count);
                Assert.AreEqual(expectedSourceId, targetReloaded.Sources.First().Id);
            }
        }
        public void ShouldAddTwoAssociatesWithSharedRequiredAssociate()
        {
            var root = new RootEntity { RequiredAssociate = new RequiredAssociate(), Sources = new List<RootEntity>() };
            var requiredAssociate = new RequiredAssociate();
            using (var context = new TestDbContext())
            {
                context.RootEntities.Add(root);
                context.RequiredAssociates.Add(requiredAssociate);
                context.SaveChanges();
            } // Simulate detach

            var expectedAssociateId = requiredAssociate.Id;
            var associateOne = new RootEntity { RequiredAssociate = requiredAssociate };
            root.Sources.Add(associateOne);
            var associateTwo = new RootEntity { RequiredAssociate = requiredAssociate };
            root.Sources.Add(associateTwo);

            using (var context = new TestDbContext())
            {
                root = context.UpdateGraph(root, map => map.AssociatedCollection(r => r.Sources));
                context.SaveChanges();

                Assert.IsTrue(root.Sources.All(s => s.RequiredAssociate.Id == expectedAssociateId));

                var sourceIds = root.Sources.Select(s => s.Id).ToArray();
                var sourcesReloaded = context.RootEntities.Where(r => sourceIds.Contains(r.Id)).ToList();
                Assert.IsTrue(sourcesReloaded.All(s => s.RequiredAssociate != null && s.RequiredAssociate.Id == expectedAssociateId));
            }
        }