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"); } }
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); } }
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); } }
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, 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 owned = node2.OneToManyOwned.First(); Assert.IsTrue(owned.OneParent == node2 && owned.Title == "What's up"); } }
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(); } }
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); } }
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)); } }
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(); } }
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"); } }
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()) { // 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 == "Newer Entity"); } }
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(); } }
public void ShouldRemoveItemsInOwnedCollectionWhenSetToNull() { 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 = null; 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); } }
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()) { // Setup mapping context.UpdateGraph(node1, map => map .OwnedEntity(p => p.OneToOneOwned, with => with .OwnedEntity(p => p.OneToOneOneToOneOwned) .AssociatedEntity(p => p.OneToOneOneToOneAssociated) .OwnedCollection(p => p.OneToOneOneToManyOwned) .AssociatedCollection(p => p.OneToOneOneToManyAssociated))); 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"); } }
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 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, map => map.OwnedEntity(p => p.OneToOneOwned, with => with.OwnedCollection(p => p.OneToOneOneToManyOwned))); 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); } }
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()) { // Setup mapping context.UpdateGraph(node1, map => map .OwnedCollection(p => p.OneToManyOwned, with => with .OwnedEntity(p => p.OneToManyOneToOneOwned) .AssociatedEntity(p => p.OneToManyOneToOneAssociated) .OwnedCollection(p => p.OneToManyOneToManyOwned) .AssociatedCollection(p => p.OneToManyOneToManyAssociated))); 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); } } }
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()) { // 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(node2.OneToOneAssociated.Title == "Associated Node"); } }
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()) { node1 = 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.AreEqual(2, node2.OneToManyOwned.Count); var ownedId = node1.OneToManyOwned.Skip(1).Select(o => o.Id).Single(); var owned = context.OneToManyOwnedModels.Single(p => p.Id == ownedId); Assert.IsTrue(owned.OneParent == node2 && owned.Title == "Hi"); } }
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"); } }
public void ShouldRemoveEntityIfRemovedFromParent() { 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()) { // 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.IsNull(context.OneToOneOwnedModels.SingleOrDefault(p => p.Id == oneToOne.Id)); } }