Пример #1
0
        public void ShouldUpdateOwnedCollectionElementWithTwoParents()
        {
            var firstParent = new OneToManyOwnedParentModel {
                Title = "First Parent"
            };
            var secondParent = new OneToManyOwnedParentModel {
                Title = "Second Parent"
            };
            var child = new OneToManyOwnedMultipleParentsModel {
                FirstParent = firstParent, SecondParent = secondParent, Title = "Child"
            };

            firstParent.OneToManyOwnedMultipleParentsModels = new List <OneToManyOwnedMultipleParentsModel> {
                child
            };

            using (var context = new TestDbContext())
            {
                context.OneToManyOwnedParentModels.Add(firstParent);
                context.OneToManyOwnedParentModels.Add(secondParent);
                context.SaveChanges();

                firstParent = context.OneToManyOwnedParentModels
                              .Include(o => o.OneToManyOwnedMultipleParentsModels)
                              .Single(o => o.Id == firstParent.Id);
            }

            const string updatedChildTitle = "Updated Child";

            firstParent.OneToManyOwnedMultipleParentsModels.Single().Title = updatedChildTitle;

            using (var context = new TestDbContext())
            {
                var attachedItem = context.UpdateGraph(firstParent,
                                                       a => a.OwnedCollection(b => b.OneToManyOwnedMultipleParentsModels,
                                                                              with => with.AssociatedEntity(d => d.SecondParent)));
                context.SaveChanges();

                Assert.AreEqual(1, attachedItem.OneToManyOwnedMultipleParentsModels.Count);

                var attachedChild = attachedItem.OneToManyOwnedMultipleParentsModels.Single();
                Assert.AreEqual(updatedChildTitle, attachedChild.Title);
                Assert.IsNotNull(attachedChild.FirstParent);
                Assert.AreEqual(attachedItem.Id, attachedChild.FirstParent.Id);

                Assert.IsNotNull(attachedChild.SecondParent);
                Assert.AreEqual(secondParent.Id, attachedChild.SecondParent.Id);
            }
        }
        public void ShouldThrowBecauseOfAmbiguousUnmappedParentRelation()
        {
            var firstParent = new OneToManyOwnedParentModel {
                Title = "First Parent"
            };
            var child = new OneToManyOwnedMultipleParentsModel {
                FirstParent = firstParent, Title = "Child"
            };

            firstParent.OneToManyOwnedMultipleParentsModels = new List <OneToManyOwnedMultipleParentsModel> {
                child
            };

            using (var context = new TestDbContext())
            {
                context.UpdateGraph(firstParent, a => a.OwnedCollection(b => b.OneToManyOwnedMultipleParentsModels));
            }
        }