public void ShouldAddOneAssociateToMany() { var associate = new ManyToOneModel { Title = "TheOne" }; var firstParent = new TestNode { ManyToOneAssociated = associate }; using (var context = new TestDbContext()) { context.ManyToOneModels.Add(associate); context.Nodes.Add(firstParent); context.SaveChanges(); } // Simulate detach var secondParent = new TestNode { ManyToOneAssociated = associate }; using (var context = new TestDbContext()) { secondParent = context.UpdateGraph(secondParent, map => map.AssociatedEntity(e => e.ManyToOneAssociated)); context.SaveChanges(); Assert.IsNotNull(secondParent.ManyToOneAssociated); Assert.AreEqual(associate.Id, secondParent.ManyToOneAssociated.Id); Assert.AreEqual(1, context.ManyToOneModels.Count()); } }
public void ShouldRemoveOneAssociateFromMany() { var associate = new ManyToOneModel { Title = "TheOne" }; var firstParent = new TestNode { ManyToOneAssociated = associate }; var secondParent = new TestNode { ManyToOneAssociated = associate }; using (var context = new TestDbContext()) { context.ManyToOneModels.Add(associate); context.Nodes.Add(firstParent); context.Nodes.Add(secondParent); context.SaveChanges(); Assert.AreEqual(1, context.ManyToOneModels.Count()); } // Simulate detach firstParent.ManyToOneAssociated = null; using (var context = new TestDbContext()) { firstParent = context.UpdateGraph(firstParent, map => map.AssociatedEntity(e => e.ManyToOneAssociated)); context.SaveChanges(); firstParent = context.Nodes.Include(n => n.ManyToOneAssociated).Single(n => n.Id == firstParent.Id); Assert.IsNull(firstParent.ManyToOneAssociated); Assert.AreEqual(1, context.ManyToOneModels.Count()); secondParent = context.Nodes.Include(n => n.ManyToOneAssociated).Single(n => n.Id == secondParent.Id); Assert.IsNotNull(secondParent.ManyToOneAssociated); Assert.AreEqual(associate.Id, secondParent.ManyToOneAssociated.Id); } secondParent.ManyToOneAssociated = null; using (var context = new TestDbContext()) { secondParent = context.UpdateGraph(secondParent, map => map.AssociatedEntity(e => e.ManyToOneAssociated)); context.SaveChanges(); secondParent = context.Nodes.Include(n => n.ManyToOneAssociated).Single(n => n.Id == secondParent.Id); Assert.IsNull(secondParent.ManyToOneAssociated); Assert.AreEqual(1, context.ManyToOneModels.Count()); } }