public void Add_dependent_then_principal_one_to_many_FK_set_both_navs_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new Category { Id = 77 }; var dependent = new Product { Id = 78, Category = principal }; principal.Products.Add(dependent); context.Entry(dependent).Property("CategoryId").CurrentValue = principal.Id; context.Entry(dependent).State = entityState; context.Entry(principal).State = entityState; AssertFixup( context, () => { Assert.Equal(principal.Id, context.Entry(dependent).Property("CategoryId").CurrentValue); Assert.Same(principal, dependent.Category); Assert.Equal(new[] { dependent }.ToList(), principal.Products); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); }); } }
public async Task Principal_nav_set_unidirectional_AddAsync() { using (var context = new FixupContext()) { var principal = new ParentPN { Id = 77 }; var dependent = new ChildPN { Name = "1" }; principal.Child1 = dependent; await context.AddAsync(principal); var entityState = EntityState.Added; Assert.Equal(2, context.ChangeTracker.Entries().Count()); AssertFixup( context, () => { Assert.Equal(principal.Id, context.Entry(dependent).Property("ParentId").CurrentValue); Assert.Same(dependent, principal.Child1); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); }); } }
public void Navigation_fixup_happens_when_new_entities_are_tracked() { using (var context = new FixupContext()) { context.Add(new Category { Id = 11 }); context.Add(new Category { Id = 12 }); context.Add(new Category { Id = 13 }); context.Add(new Product { Id = 21, CategoryId = 11 }); AssertAllFixedUp(context); context.Add(new Product { Id = 22, CategoryId = 11 }); AssertAllFixedUp(context); context.Add(new Product { Id = 23, CategoryId = 11 }); AssertAllFixedUp(context); context.Add(new Product { Id = 24, CategoryId = 12 }); AssertAllFixedUp(context); context.Add(new Product { Id = 25, CategoryId = 12 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 31, ProductId = 22 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 32, ProductId = 22 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 33, ProductId = 24 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 34, ProductId = 24 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 35, ProductId = 24 }); AssertAllFixedUp(context); Assert.Equal(3, context.ChangeTracker.Entries<Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<SpecialOffer>().Count()); } }
public void Replacing_owned_entity_throws(EntityState entityState) { using (var context = new FixupContext()) { var principal = new ParentPN { Id = 77 }; var dependent1 = new ChildPN { Name = "1" }; principal.Child1 = dependent1; var dependent2 = new ChildPN { Name = "2" }; context.ChangeTracker.TrackGraph(principal, e => e.Entry.State = entityState); principal.Child1 = dependent2; Assert.Equal( CoreStrings.IdentityConflictOwned("ParentPN.Child1#ChildPN", "{'ParentId'}"), Assert.Throws <InvalidOperationException>( () => context.ChangeTracker.DetectChanges()).Message); } }
public void Can_get_owned_entity_entry() { using (var context = new FixupContext()) { var principal = new ParentPN { Id = 77 }; var dependent = new ChildPN { Name = "1" }; principal.Child1 = dependent; principal.Child2 = dependent; Assert.Equal( CoreStrings.UntrackedDependentEntity( typeof(ChildPN).ShortDisplayName(), "." + nameof(EntityEntry.Reference) + "()." + nameof(ReferenceEntry.TargetEntry)), Assert.Throws <InvalidOperationException>(() => context.Entry(dependent)).Message); var dependentEntry1 = context.Entry(principal).Reference(p => p.Child1).TargetEntry; Assert.Same(dependentEntry1.GetInfrastructure(), context.Entry(dependent).GetInfrastructure()); var dependentEntry2 = context.Entry(principal).Reference(p => p.Child2).TargetEntry; Assert.NotNull(dependentEntry2); Assert.Equal( CoreStrings.AmbiguousDependentEntity( typeof(ChildPN).ShortDisplayName(), "." + nameof(EntityEntry.Reference) + "()." + nameof(ReferenceEntry.TargetEntry)), Assert.Throws <InvalidOperationException>(() => context.Entry(dependent)).Message); } }
public void Identity_changed_bidirectional(EntityState entityState) { using (var context = new FixupContext()) { var principal = new Parent { Id = 77 }; var dependent = new Child { Name = "1", Parent = principal }; principal.Child2 = dependent; context.ChangeTracker.TrackGraph(principal, e => e.Entry.State = entityState); var dependentEntry1 = context.Entry(principal).Reference(p => p.Child2).TargetEntry; principal.Child1 = dependent; principal.Child2 = null; context.ChangeTracker.DetectChanges(); Assert.Equal(entityState == EntityState.Added ? 2 : 3, context.ChangeTracker.Entries().Count()); Assert.Null(principal.Child2); Assert.Same(principal, dependent.Parent); Assert.Same(dependent, principal.Child1); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState == EntityState.Added ? EntityState.Detached : EntityState.Deleted, dependentEntry1.State); var dependentEntry = context.Entry(principal).Reference(p => p.Child1).TargetEntry; Assert.Equal(principal.Id, dependentEntry.Property("ParentId").CurrentValue); Assert.Equal(EntityState.Added, dependentEntry.State); Assert.Equal(nameof(Parent.Child1), dependentEntry.Metadata.DefiningNavigationName); } }
public void Adding_duplicate_owned_entity_throws_by_default() { using (var context = new FixupContext(false)) { var principal = new ParentPN { Id = 77 }; var dependent = new ChildPN { Name = "1" }; principal.Child1 = dependent; principal.Child2 = dependent; var dependentEntry1 = context.Entry(principal).Reference(p => p.Child1).TargetEntry; Assert.Same(dependentEntry1.GetInfrastructure(), context.Entry(dependent).GetInfrastructure()); Assert.Equal( CoreStrings.WarningAsErrorTemplate( CoreEventId.DuplicateDependentEntityTypeInstanceWarning.ToString(), CoreStrings.LogDuplicateDependentEntityTypeInstance.GenerateMessage( typeof(ParentPN).ShortDisplayName() + "." + nameof(ParentPN.Child2) + "#" + typeof(ChildPN).ShortDisplayName(), typeof(ParentPN).ShortDisplayName() + "." + nameof(ParentPN.Child1) + "#" + typeof(ChildPN).ShortDisplayName()), "CoreEventId.DuplicateDependentEntityTypeInstanceWarning"), Assert.Throws <InvalidOperationException>(() => context.Entry(principal).Reference(p => p.Child2).TargetEntry).Message); } }
public void Parent_and_identity_swapped_bidirectional(EntityState entityState) { using (var context = new FixupContext()) { var principal1 = new Parent { Id = 77 }; var principal2 = new Parent { Id = 78 }; var dependent1 = new Child { Name = "1", Parent = principal1 }; principal1.Child2 = dependent1; var dependent2 = new Child { Name = "2" }; principal2.Child1 = dependent2; context.ChangeTracker.TrackGraph(principal1, e => e.Entry.State = entityState); context.ChangeTracker.TrackGraph(principal2, e => e.Entry.State = entityState); principal2.Child1 = dependent1; principal1.Child2 = dependent2; context.ChangeTracker.DetectChanges(); Assert.Equal(4, context.ChangeTracker.Entries().Count()); Assert.Null(principal1.Child1); Assert.Same(dependent2, principal1.Child2); Assert.Same(dependent1, principal2.Child1); Assert.Null(principal2.Child2); Assert.Same(principal2, dependent1.Parent); Assert.Same(principal1, dependent2.Parent); Assert.Equal(entityState, context.Entry(principal1).State); Assert.Equal(entityState, context.Entry(principal2).State); var dependent1Entry = context.Entry(principal1).Reference(p => p.Child2).TargetEntry; Assert.Equal(EntityState.Added, dependent1Entry.State); Assert.Equal(principal1.Id, dependent1Entry.Property("ParentId").CurrentValue); Assert.Equal(nameof(Parent.Child2), dependent1Entry.Metadata.DefiningNavigationName); Assert.Equal(entityState == EntityState.Added ? null : (EntityState?)EntityState.Deleted, dependent1Entry.GetInfrastructure().SharedIdentityEntry?.EntityState); var dependent2Entry = context.Entry(principal2).Reference(p => p.Child1).TargetEntry; Assert.Equal(principal2.Id, dependent2Entry.Property("ParentId").CurrentValue); Assert.Equal(EntityState.Added, dependent2Entry.State); Assert.Equal(nameof(Parent.Child1), dependent2Entry.Metadata.DefiningNavigationName); Assert.Equal(entityState == EntityState.Added ? null : (EntityState?)EntityState.Deleted, dependent1Entry.GetInfrastructure().SharedIdentityEntry?.EntityState); dependent1Entry.GetInfrastructure().AcceptChanges(); dependent2Entry.GetInfrastructure().AcceptChanges(); Assert.Null(dependent1Entry.GetInfrastructure().SharedIdentityEntry); Assert.Null(dependent2Entry.GetInfrastructure().SharedIdentityEntry); } }
public void Parent_swapped_bidirectional(EntityState entityState) { using (var context = new FixupContext()) { var principal1 = new Parent { Id = 77 }; var principal2 = new Parent { Id = 78 }; var dependent1 = new Child { Name = "1" }; principal1.Child1 = dependent1; var dependent2 = new Child { Name = "2" }; principal2.Child1 = dependent2; context.ChangeTracker.TrackGraph(principal1, e => e.Entry.State = entityState); context.ChangeTracker.TrackGraph(principal2, e => e.Entry.State = entityState); principal1.Child1 = dependent2; principal2.Child1 = dependent1; if (entityState != EntityState.Added) { Assert.Equal( CoreStrings.KeyReadOnly("ParentId", context.Entry(principal1).Reference(p => p.Child1).Metadata.GetTargetType().DisplayName()), Assert.Throws <InvalidOperationException>(() => context.ChangeTracker.DetectChanges()).Message); } else { context.ChangeTracker.DetectChanges(); Assert.Equal(4, context.ChangeTracker.Entries().Count()); Assert.Same(dependent2, principal1.Child1); Assert.Null(principal1.Child2); Assert.Same(dependent1, principal2.Child1); Assert.Null(principal2.Child2); Assert.Same(principal2, dependent1.Parent); Assert.Same(principal1, dependent2.Parent); Assert.Equal(entityState, context.Entry(principal1).State); Assert.Equal(entityState, context.Entry(principal2).State); var dependent1Entry = context.Entry(principal1).Reference(p => p.Child1).TargetEntry; Assert.Equal(entityState == EntityState.Added ? EntityState.Added : EntityState.Modified, dependent1Entry.State); Assert.Equal(principal1.Id, dependent1Entry.Property("ParentId").CurrentValue); Assert.Equal(nameof(Parent.Child1), dependent1Entry.Metadata.DefiningNavigationName); var dependent2Entry = context.Entry(principal2).Reference(p => p.Child1).TargetEntry; Assert.Equal(principal2.Id, dependent2Entry.Property("ParentId").CurrentValue); Assert.Equal(entityState == EntityState.Added ? EntityState.Added : EntityState.Modified, dependent2Entry.State); Assert.Equal(nameof(Parent.Child1), dependent2Entry.Metadata.DefiningNavigationName); } } }
private void Principal_nav_set_bidirectional_impl(EntityState entityState, bool?graph) { using (var context = new FixupContext()) { var principal = new Parent { Id = 77 }; if (graph == null) { context.Entry(principal).State = entityState; } var dependent = new Child { Name = "1" }; principal.Child1 = dependent; if (graph == null) { context.ChangeTracker.DetectChanges(); } else if (graph == true) { context.ChangeTracker.TrackGraph(principal, e => e.Entry.State = entityState); } else { switch (entityState) { case EntityState.Added: context.Add(principal); break; case EntityState.Unchanged: context.Attach(principal); break; case EntityState.Modified: context.Update(principal); break; } } Assert.Equal(2, context.ChangeTracker.Entries().Count()); AssertFixup( context, () => { Assert.Equal(principal.Id, context.Entry(dependent).Property("ParentId").CurrentValue); Assert.Same(dependent, principal.Child1); Assert.Same(principal, dependent.Parent); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(graph == null ? EntityState.Added : entityState, context.Entry(dependent).State); }); } }
private void Add_principal_and_dependent_one_to_many( EntityState entityState, bool principalFirst, bool setFk, bool setToPrincipal, bool setToDependent) { using var context = new FixupContext(); var principal = new Category(77); var dependent = new Product(78); var principalEntry = context.Entry(principal); var dependentEntry = context.Entry(dependent); if (setFk) { dependentEntry.Property("CategoryId").CurrentValue = principal.Id; } if (setToPrincipal) { dependentEntry.Navigation("Category").CurrentValue = principal; } if (setToDependent) { var collection = new HashSet <object> { dependent }; principalEntry.Collection("Products").CurrentValue = collection; } if (principalFirst) { principalEntry.State = entityState; } dependentEntry.State = entityState; if (!principalFirst) { principalEntry.State = entityState; } AssertFixup( context, () => { Assert.Equal(principal.Id, dependentEntry.Property("CategoryId").CurrentValue); Assert.Same(principal, dependentEntry.Navigation("Category").CurrentValue); Assert.Equal(new[] { dependent }, principalEntry.Collection("Products").CurrentValue); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); }); }
public void Parent_swapped_unidirectional(EntityState entityState) { using (var context = new FixupContext()) { var principal1 = new ParentPN { Id = 77 }; var principal2 = new ParentPN { Id = 78 }; var dependent1 = new ChildPN { Name = "1" }; principal1.Child1 = dependent1; var dependent2 = new ChildPN { Name = "2" }; principal2.Child1 = dependent2; context.ChangeTracker.TrackGraph(principal1, e => e.Entry.State = entityState); context.ChangeTracker.TrackGraph(principal2, e => e.Entry.State = entityState); var dependent1Entry = context.Entry(principal1).Reference(p => p.Child1).TargetEntry; var dependent2Entry = context.Entry(principal2).Reference(p => p.Child1).TargetEntry; principal1.Child1 = dependent2; principal2.Child1 = dependent1; context.ChangeTracker.DetectChanges(); Assert.Equal(4, context.ChangeTracker.Entries().Count()); Assert.Same(dependent2, principal1.Child1); Assert.Null(principal1.Child2); Assert.Same(dependent1, principal2.Child1); Assert.Null(principal2.Child2); Assert.Equal(entityState, context.Entry(principal1).State); Assert.Equal(entityState, context.Entry(principal2).State); Assert.Equal(EntityState.Detached, dependent1Entry.State); Assert.Equal(EntityState.Detached, dependent2Entry.State); Assert.Same( dependent1Entry.GetInfrastructure(), context.Entry(principal2).Reference(p => p.Child1).TargetEntry.GetInfrastructure()); Assert.Equal(principal2.Id, dependent1Entry.Property("ParentId").CurrentValue); Assert.Equal(EntityState.Modified, dependent1Entry.State); Assert.Equal(nameof(Parent.Child1), dependent1Entry.Metadata.DefiningNavigationName); Assert.Same( dependent2Entry.GetInfrastructure(), context.Entry(principal1).Reference(p => p.Child1).TargetEntry.GetInfrastructure()); Assert.Equal(principal1.Id, dependent2Entry.Property("ParentId").CurrentValue); Assert.Equal(EntityState.Modified, dependent2Entry.State); Assert.Equal(nameof(Parent.Child1), dependent2Entry.Metadata.DefiningNavigationName); } }
private void Add_principal_and_dependent_one_to_one( EntityState entityState, bool principalFirst, bool setFk, bool setToPrincipal, bool setToDependent) { using var context = new FixupContext(); var principal = new Parent(77); var dependent = new Child(78); var principalEntry = context.Entry(principal); var dependentEntry = context.Entry(dependent); if (setFk) { dependentEntry.Property("ParentId").CurrentValue = principal.Id; } if (setToPrincipal) { dependentEntry.Navigation("Parent").CurrentValue = principal; } if (setToDependent) { principalEntry.Navigation("Child").CurrentValue = dependent; } if (principalFirst) { principalEntry.State = entityState; } dependentEntry.State = entityState; if (!principalFirst) { principalEntry.State = entityState; } AssertFixup( context, () => { Assert.Equal(principal.Id, dependentEntry.Property("ParentId").CurrentValue); Assert.Same(principal, dependentEntry.Navigation("Parent").CurrentValue); Assert.Same(dependent, principalEntry.Navigation("Child").CurrentValue); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); }); }
public void Parent_changed_unidirectional(EntityState entityState) { using (var context = new FixupContext()) { var principal1 = new ParentPN { Id = 77 }; var principal2 = new ParentPN { Id = 78 }; var dependent = new ChildPN { Name = "1" }; principal1.Child1 = dependent; context.ChangeTracker.TrackGraph(principal1, e => e.Entry.State = entityState); context.ChangeTracker.TrackGraph(principal2, e => e.Entry.State = entityState); var dependentEntry1 = context.Entry(principal1).Reference(p => p.Child1).TargetEntry; principal2.Child1 = dependent; principal1.Child1 = null; if (entityState != EntityState.Added) { Assert.Equal( CoreStrings.KeyReadOnly("ParentId", dependentEntry1.Metadata.DisplayName()), Assert.Throws <InvalidOperationException>(() => context.ChangeTracker.DetectChanges()).Message); } else { context.ChangeTracker.DetectChanges(); Assert.Equal(3, context.ChangeTracker.Entries().Count()); Assert.Null(principal1.Child1); Assert.Null(principal1.Child2); Assert.Same(dependent, principal2.Child1); Assert.Null(principal2.Child2); Assert.Equal(entityState, context.Entry(principal1).State); Assert.Equal(entityState, context.Entry(principal2).State); Assert.Equal(EntityState.Detached, dependentEntry1.State); var dependentEntry2 = context.Entry(principal2).Reference(p => p.Child1).TargetEntry; Assert.Equal(principal2.Id, dependentEntry2.Property("ParentId").CurrentValue); Assert.Equal(EntityState.Added, dependentEntry2.State); Assert.Equal(nameof(Parent.Child1), dependentEntry2.Metadata.DefiningNavigationName); } } }
public void Add_dependent_then_principal_one_to_many_FK_set_no_navs_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new Category { Id = 77 }; var dependent = new Product { Id = 78, CategoryId = principal.Id }; context.Entry(dependent).State = entityState; context.Entry(principal).State = entityState; Assert.Equal(principal.Id, dependent.CategoryId); Assert.Same(principal, dependent.Category); Assert.Equal(new[] { dependent }.ToList(), principal.Products); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); } }
public void Identity_swapped_bidirectional(EntityState entityState) { using (var context = new FixupContext()) { var principal = new Parent { Id = 77 }; var dependent1 = new Child { Name = "1", Parent = principal }; principal.Child1 = dependent1; var dependent2 = new Child { Name = "2", Parent = principal }; principal.Child2 = dependent2; context.ChangeTracker.TrackGraph(principal, e => e.Entry.State = entityState); var dependent1Entry = context.Entry(principal).Reference(p => p.Child1).TargetEntry; var dependent2Entry = context.Entry(principal).Reference(p => p.Child2).TargetEntry; principal.Child2 = dependent1; principal.Child1 = dependent2; context.ChangeTracker.DetectChanges(); Assert.Equal(3, context.ChangeTracker.Entries().Count()); Assert.Same(principal, dependent1.Parent); Assert.Same(dependent1, principal.Child2); Assert.Same(principal, dependent2.Parent); Assert.Same(dependent2, principal.Child1); Assert.Equal(entityState, context.Entry(principal).State); Assert.Same( dependent1Entry.GetInfrastructure(), context.Entry(principal).Reference(p => p.Child1).TargetEntry.GetInfrastructure()); Assert.Equal(principal.Id, dependent1Entry.Property("ParentId").CurrentValue); Assert.Equal(EntityState.Modified, dependent1Entry.State); Assert.Equal(nameof(Parent.Child1), dependent1Entry.Metadata.DefiningNavigationName); Assert.Same( dependent2Entry.GetInfrastructure(), context.Entry(principal).Reference(p => p.Child2).TargetEntry.GetInfrastructure()); Assert.Equal(principal.Id, dependent2Entry.Property("ParentId").CurrentValue); Assert.Equal(EntityState.Modified, dependent2Entry.State); Assert.Equal(nameof(Parent.Child2), dependent2Entry.Metadata.DefiningNavigationName); } }
public void Navigation_fixup_happens_when_entities_are_tracked_from_query() { using (var context = new FixupContext()) { var categoryType = context.Model.GetEntityType(typeof(Category)); var productType = context.Model.GetEntityType(typeof(Product)); var offerType = context.Model.GetEntityType(typeof(SpecialOffer)); var stateManager = context.ChangeTracker.GetService(); stateManager.StartTracking(categoryType, new SimpleEntityKey<int>(categoryType.FindPrimaryKey(), 11), new Category { Id = 11 }, new ValueBuffer(new object[] { 11 })); stateManager.StartTracking(categoryType, new SimpleEntityKey<int>(categoryType.FindPrimaryKey(), 12), new Category { Id = 12 }, new ValueBuffer(new object[] { 12 })); stateManager.StartTracking(categoryType, new SimpleEntityKey<int>(categoryType.FindPrimaryKey(), 13), new Category { Id = 13 }, new ValueBuffer(new object[] { 13 })); stateManager.StartTracking(productType, new SimpleEntityKey<int>(productType.FindPrimaryKey(), 21), new Product { Id = 21, CategoryId = 11 }, new ValueBuffer(new object[] { 21, 11 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new SimpleEntityKey<int>(productType.FindPrimaryKey(), 22), new Product { Id = 22, CategoryId = 11 }, new ValueBuffer(new object[] { 22, 11 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new SimpleEntityKey<int>(productType.FindPrimaryKey(), 23), new Product { Id = 23, CategoryId = 11 }, new ValueBuffer(new object[] { 23, 11 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new SimpleEntityKey<int>(productType.FindPrimaryKey(), 24), new Product { Id = 24, CategoryId = 12 }, new ValueBuffer(new object[] { 24, 12 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new SimpleEntityKey<int>(productType.FindPrimaryKey(), 25), new Product { Id = 25, CategoryId = 12 }, new ValueBuffer(new object[] { 25, 12 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey<int>(offerType.FindPrimaryKey(), 31), new SpecialOffer { Id = 31, ProductId = 22 }, new ValueBuffer(new object[] { 31, 22 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey<int>(offerType.FindPrimaryKey(), 32), new SpecialOffer { Id = 32, ProductId = 22 }, new ValueBuffer(new object[] { 32, 22 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey<int>(offerType.FindPrimaryKey(), 33), new SpecialOffer { Id = 33, ProductId = 24 }, new ValueBuffer(new object[] { 33, 24 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey<int>(offerType.FindPrimaryKey(), 34), new SpecialOffer { Id = 34, ProductId = 24 }, new ValueBuffer(new object[] { 34, 24 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey<int>(offerType.FindPrimaryKey(), 35), new SpecialOffer { Id = 35, ProductId = 24 }, new ValueBuffer(new object[] { 35, 24 })); AssertAllFixedUp(context); Assert.Equal(3, context.ChangeTracker.Entries<Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<SpecialOffer>().Count()); } }
public void Navigation_fixup_happens_when_entities_are_materialized() { using (var context = new FixupContext()) { var categoryType = context.Model.GetEntityType(typeof(Category)); var productType = context.Model.GetEntityType(typeof(Product)); var offerType = context.Model.GetEntityType(typeof(SpecialOffer)); var stateManager = context.ChangeTracker.StateManager; stateManager.GetOrMaterializeEntry(categoryType, new ObjectArrayValueReader(new object[] { 11 })); stateManager.GetOrMaterializeEntry(categoryType, new ObjectArrayValueReader(new object[] { 12 })); stateManager.GetOrMaterializeEntry(categoryType, new ObjectArrayValueReader(new object[] { 13 })); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 11, 21 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 11, 22 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 11, 23 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 12, 24 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 12, 25 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 31, 22 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 32, 22 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 33, 24 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 34, 24 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 35, 24 })); AssertAllFixedUp(context); Assert.Equal(3, context.ChangeTracker.Entries <Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <SpecialOffer>().Count()); } }
public void Navigation_fixup_happens_when_entities_are_materialized() { using (var context = new FixupContext()) { var categoryType = context.Model.GetEntityType(typeof(Category)); var productType = context.Model.GetEntityType(typeof(Product)); var offerType = context.Model.GetEntityType(typeof(SpecialOffer)); var stateManager = context.ChangeTracker.StateManager; stateManager.GetOrMaterializeEntry(categoryType, new ObjectArrayValueReader(new object[] { 11 })); stateManager.GetOrMaterializeEntry(categoryType, new ObjectArrayValueReader(new object[] { 12 })); stateManager.GetOrMaterializeEntry(categoryType, new ObjectArrayValueReader(new object[] { 13 })); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 11, 21 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 11, 22 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 11, 23 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 12, 24 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(productType, new ObjectArrayValueReader(new object[] { 12, 25 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 31, 22 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 32, 22 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 33, 24 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 34, 24 })); AssertAllFixedUp(context); stateManager.GetOrMaterializeEntry(offerType, new ObjectArrayValueReader(new object[] { 35, 24 })); AssertAllFixedUp(context); Assert.Equal(3, context.ChangeTracker.Entries<Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<SpecialOffer>().Count()); } }
public void Parent_and_identity_changed_unidirectional(EntityState entityState) { using (var context = new FixupContext()) { var principal1 = new ParentPN { Id = 77 }; var principal2 = new ParentPN { Id = 78 }; var dependent = new ChildPN { Name = "1" }; principal1.Child2 = dependent; context.ChangeTracker.TrackGraph(principal1, e => e.Entry.State = entityState); context.ChangeTracker.TrackGraph(principal2, e => e.Entry.State = entityState); var dependentEntry1 = context.Entry(principal1).Reference(p => p.Child2).TargetEntry; principal2.Child1 = dependent; principal1.Child2 = null; context.ChangeTracker.DetectChanges(); Assert.Equal(entityState == EntityState.Added ? 3 : 4, context.ChangeTracker.Entries().Count()); Assert.Null(principal1.Child1); Assert.Null(principal1.Child2); Assert.Same(dependent, principal2.Child1); Assert.Null(principal2.Child2); Assert.Equal(entityState, context.Entry(principal1).State); Assert.Equal(entityState, context.Entry(principal2).State); Assert.Equal(entityState == EntityState.Added ? EntityState.Detached : EntityState.Deleted, dependentEntry1.State); var dependentEntry2 = context.Entry(principal2).Reference(p => p.Child1).TargetEntry; Assert.Equal(principal2.Id, dependentEntry2.Property("ParentId").CurrentValue); Assert.Equal(EntityState.Added, dependentEntry2.State); Assert.Equal(nameof(Parent.Child1), dependentEntry2.Metadata.DefiningNavigationName); } }
public void Add_principal_but_not_dependent_one_to_one_prin_uni_FK_not_set_principal_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new ParentPN { Id = 77 }; var dependent = new ChildPN { Id = 78 }; context.Entry(principal).State = entityState; principal.Child = dependent; context.ChangeTracker.DetectChanges(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentId); Assert.Same(dependent, principal.Child); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(EntityState.Added, context.Entry(dependent).State); }); } }
public void Navigation_fixup_happens_when_entities_are_tracked_from_query() { using (var context = new FixupContext()) { var categoryType = context.Model.GetEntityType(typeof(Category)); var productType = context.Model.GetEntityType(typeof(Product)); var offerType = context.Model.GetEntityType(typeof(SpecialOffer)); var stateManager = context.ChangeTracker.StateManager; stateManager.StartTracking(categoryType, new Category { Id = 11 }, new ObjectArrayValueReader(new object[] { 11 })); stateManager.StartTracking(categoryType, new Category { Id = 12 }, new ObjectArrayValueReader(new object[] { 12 })); stateManager.StartTracking(categoryType, new Category { Id = 13 }, new ObjectArrayValueReader(new object[] { 13 })); stateManager.StartTracking(productType, new Product { Id = 21, CategoryId = 11 }, new ObjectArrayValueReader(new object[] { 11, 21 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new Product { Id = 22, CategoryId = 11 }, new ObjectArrayValueReader(new object[] { 11, 22 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new Product { Id = 23, CategoryId = 11 }, new ObjectArrayValueReader(new object[] { 11, 23 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new Product { Id = 24, CategoryId = 12 }, new ObjectArrayValueReader(new object[] { 12, 24 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new Product { Id = 25, CategoryId = 12 }, new ObjectArrayValueReader(new object[] { 12, 25 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SpecialOffer { Id = 31, ProductId = 22 }, new ObjectArrayValueReader(new object[] { 31, 22 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SpecialOffer { Id = 32, ProductId = 22 }, new ObjectArrayValueReader(new object[] { 32, 22 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SpecialOffer { Id = 33, ProductId = 24 }, new ObjectArrayValueReader(new object[] { 33, 24 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SpecialOffer { Id = 34, ProductId = 24 }, new ObjectArrayValueReader(new object[] { 34, 24 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SpecialOffer { Id = 35, ProductId = 24 }, new ObjectArrayValueReader(new object[] { 35, 24 })); AssertAllFixedUp(context); Assert.Equal(3, context.ChangeTracker.Entries <Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <SpecialOffer>().Count()); } }
private void Principal_nav_set_unidirectional_impl(EntityState entityState, bool?graph) { using (var context = new FixupContext()) { var principal = new ParentPN { Id = 77 }; if (graph == null) { context.Entry(principal).State = entityState; } var dependent = new ChildPN { Name = "1" }; principal.Child1 = dependent; var subDependent = new SubChildPN { Name = "1S" }; dependent.SubChild = subDependent; if (graph == null) { context.ChangeTracker.DetectChanges(); } else if (graph == true) { context.ChangeTracker.TrackGraph(principal, e => e.Entry.State = entityState); } else { switch (entityState) { case EntityState.Added: context.Add(principal); break; case EntityState.Unchanged: context.Attach(principal); break; case EntityState.Modified: context.Update(principal); break; } } Assert.Equal(3, context.ChangeTracker.Entries().Count()); AssertFixup( context, () => { Assert.Equal(entityState, context.Entry(principal).State); Assert.Same(dependent, principal.Child1); Assert.Null(principal.Child2); var dependentEntry = context.Entry(dependent); Assert.Equal(principal.Id, dependentEntry.Property("ParentId").CurrentValue); Assert.Equal(graph == null ? EntityState.Added : entityState, dependentEntry.State); Assert.Equal(nameof(ParentPN.Child1), dependentEntry.Metadata.DefiningNavigationName); Assert.Same(subDependent, dependent.SubChild); var subDependentEntry = context.Entry(subDependent); Assert.Equal(principal.Id, subDependentEntry.Property("ChildId").CurrentValue); Assert.Equal(graph == null ? EntityState.Added : entityState, subDependentEntry.State); Assert.Equal(nameof(ChildPN.SubChild), subDependentEntry.Metadata.DefiningNavigationName); }); } }
public void Add_dependent_but_not_principal_one_to_one_dep_uni_FK_not_set_dependent_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new ParentDN { Id = 77 }; var dependent = new ChildDN { Id = 78 }; context.Entry(dependent).State = entityState; dependent.Parent = principal; context.ChangeTracker.DetectChanges(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentId); Assert.Same(principal, dependent.Parent); Assert.Equal(EntityState.Added, context.Entry(principal).State); Assert.Equal(entityState == EntityState.Added ? EntityState.Added : EntityState.Modified, context.Entry(dependent).State); }); } }
public void Navigation_fixup_happens_when_new_entities_are_tracked() { using (var context = new FixupContext()) { context.Add(new Category { Id = 11 }); context.Add(new Category { Id = 12 }); context.Add(new Category { Id = 13 }); context.Add(new Product { Id = 21, CategoryId = 11 }); AssertAllFixedUp(context); context.Add(new Product { Id = 22, CategoryId = 11 }); AssertAllFixedUp(context); context.Add(new Product { Id = 23, CategoryId = 11 }); AssertAllFixedUp(context); context.Add(new Product { Id = 24, CategoryId = 12 }); AssertAllFixedUp(context); context.Add(new Product { Id = 25, CategoryId = 12 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 31, ProductId = 22 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 32, ProductId = 22 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 33, ProductId = 24 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 34, ProductId = 24 }); AssertAllFixedUp(context); context.Add(new SpecialOffer { Id = 35, ProductId = 24 }); AssertAllFixedUp(context); Assert.Equal(3, context.ChangeTracker.Entries <Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <SpecialOffer>().Count()); } }
public void Add_principal_then_dependent_one_to_one_FK_not_set_dependent_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new Parent { Id = 77 }; var dependent = new Child { Id = 78, Parent = principal }; context.Entry(principal).State = entityState; context.Entry(dependent).State = entityState; Assert.Equal(principal.Id, dependent.ParentId); Assert.Same(principal, dependent.Parent); Assert.Same(dependent, principal.Child); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); } }
public void Add_dependent_but_not_principal_one_to_many_no_navs_FK_set_no_navs_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new CategoryNN { Id = 77 }; var dependent = new ProductNN { Id = 78 }; context.Entry(dependent).State = entityState; dependent.CategoryId = principal.Id; context.ChangeTracker.DetectChanges(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.CategoryId); Assert.Equal(EntityState.Detached, context.Entry(principal).State); Assert.Equal(entityState == EntityState.Added ? EntityState.Added : EntityState.Modified, context.Entry(dependent).State); }); } }
public void Add_principal_but_not_dependent_one_to_many_prin_uni_FK_not_set_principal_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new CategoryPN { Id = 77 }; var dependent = new ProductPN { Id = 78 }; context.Entry(principal).State = entityState; principal.Products.Add(dependent); context.ChangeTracker.DetectChanges(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.CategoryId); Assert.Equal(new[] { dependent }.ToList(), principal.Products); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(EntityState.Added, context.Entry(dependent).State); }); } }
public void Add_dependent_then_principal_one_to_one_FK_not_set_principal_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new Parent { Id = 77 }; var dependent = new Child { Id = 78 }; principal.Child = dependent; context.Entry(dependent).State = entityState; context.Entry(principal).State = entityState; AssertFixup( context, () => { Assert.Equal(principal.Id, context.Entry(dependent).Property("ParentId").CurrentValue); Assert.Same(principal, dependent.Parent); Assert.Same(dependent, principal.Child); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); }); } }
public void Add_principal_then_dependent_one_to_many_no_navs_FK_set_no_navs_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new CategoryNN { Id = 77 }; var dependent = new ProductNN { Id = 78 }; context.Entry(dependent).Property("CategoryId").CurrentValue = principal.Id; context.Entry(principal).State = entityState; context.Entry(dependent).State = entityState; AssertFixup( context, () => { Assert.Equal(principal.Id, context.Entry(dependent).Property("CategoryId").CurrentValue); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); }); } }
public void Add_dependent_then_principal_one_to_one_no_navs_FK_set_no_navs_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new ParentNN { Id = 77 }; var dependent = new ChildNN { Id = 78, ParentId = principal.Id }; context.Entry(dependent).State = entityState; context.Entry(principal).State = entityState; Assert.Equal(principal.Id, dependent.ParentId); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); } }
public void Add_principal_then_dependent_one_to_many_prin_uni_FK_not_set_principal_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new CategoryPN { Id = 77 }; var dependent = new ProductPN { Id = 78 }; principal.Products.Add(dependent); context.Entry(principal).State = entityState; context.Entry(dependent).State = entityState; Assert.Equal(principal.Id, dependent.CategoryId); Assert.Equal(new[] { dependent }.ToList(), principal.Products); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); } }
public void Add_dependent_then_principal_one_to_many_dep_uni_FK_not_set_dependent_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new CategoryDN { Id = 77 }; var dependent = new ProductDN { Id = 78, Category = principal }; context.Entry(dependent).State = entityState; context.Entry(principal).State = entityState; Assert.Equal(principal.Id, dependent.CategoryId); Assert.Same(principal, dependent.Category); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); } }
public void Navigation_fixup_happens_when_entities_are_tracked_from_query() { using (var context = new FixupContext()) { var categoryType = context.Model.GetEntityType(typeof(Category)); var productType = context.Model.GetEntityType(typeof(Product)); var offerType = context.Model.GetEntityType(typeof(SpecialOffer)); var stateManager = context.ChangeTracker.GetService(); stateManager.StartTracking(categoryType, new SimpleEntityKey <int>(categoryType.FindPrimaryKey(), 11), new Category { Id = 11 }, new ValueBuffer(new object[] { 11 })); stateManager.StartTracking(categoryType, new SimpleEntityKey <int>(categoryType.FindPrimaryKey(), 12), new Category { Id = 12 }, new ValueBuffer(new object[] { 12 })); stateManager.StartTracking(categoryType, new SimpleEntityKey <int>(categoryType.FindPrimaryKey(), 13), new Category { Id = 13 }, new ValueBuffer(new object[] { 13 })); stateManager.StartTracking(productType, new SimpleEntityKey <int>(productType.FindPrimaryKey(), 21), new Product { Id = 21, CategoryId = 11 }, new ValueBuffer(new object[] { 21, 11 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new SimpleEntityKey <int>(productType.FindPrimaryKey(), 22), new Product { Id = 22, CategoryId = 11 }, new ValueBuffer(new object[] { 22, 11 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new SimpleEntityKey <int>(productType.FindPrimaryKey(), 23), new Product { Id = 23, CategoryId = 11 }, new ValueBuffer(new object[] { 23, 11 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new SimpleEntityKey <int>(productType.FindPrimaryKey(), 24), new Product { Id = 24, CategoryId = 12 }, new ValueBuffer(new object[] { 24, 12 })); AssertAllFixedUp(context); stateManager.StartTracking(productType, new SimpleEntityKey <int>(productType.FindPrimaryKey(), 25), new Product { Id = 25, CategoryId = 12 }, new ValueBuffer(new object[] { 25, 12 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey <int>(offerType.FindPrimaryKey(), 31), new SpecialOffer { Id = 31, ProductId = 22 }, new ValueBuffer(new object[] { 31, 22 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey <int>(offerType.FindPrimaryKey(), 32), new SpecialOffer { Id = 32, ProductId = 22 }, new ValueBuffer(new object[] { 32, 22 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey <int>(offerType.FindPrimaryKey(), 33), new SpecialOffer { Id = 33, ProductId = 24 }, new ValueBuffer(new object[] { 33, 24 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey <int>(offerType.FindPrimaryKey(), 34), new SpecialOffer { Id = 34, ProductId = 24 }, new ValueBuffer(new object[] { 34, 24 })); AssertAllFixedUp(context); stateManager.StartTracking(offerType, new SimpleEntityKey <int>(offerType.FindPrimaryKey(), 35), new SpecialOffer { Id = 35, ProductId = 24 }, new ValueBuffer(new object[] { 35, 24 })); AssertAllFixedUp(context); Assert.Equal(3, context.ChangeTracker.Entries <Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <SpecialOffer>().Count()); } }
public void Navigation_fixup_is_non_destructive_to_existing_graphs() { using (var context = new FixupContext()) { var category11 = new Category { Id = 11 }; var category12 = new Category { Id = 12 }; var category13 = new Category { Id = 13 }; var product21 = new Product { Id = 21, CategoryId = 11, Category = category11 }; var product22 = new Product { Id = 22, CategoryId = 11, Category = category11 }; var product23 = new Product { Id = 23, CategoryId = 11, Category = category11 }; var product24 = new Product { Id = 24, CategoryId = 12, Category = category12 }; var product25 = new Product { Id = 25, CategoryId = 12, Category = category12 }; category11.Products.Add(product21); category11.Products.Add(product22); category11.Products.Add(product23); category12.Products.Add(product24); category12.Products.Add(product25); var specialOffer31 = new SpecialOffer { Id = 31, ProductId = 22, Product = product22 }; var specialOffer32 = new SpecialOffer { Id = 32, ProductId = 22, Product = product22 }; var specialOffer33 = new SpecialOffer { Id = 33, ProductId = 24, Product = product24 }; var specialOffer34 = new SpecialOffer { Id = 34, ProductId = 24, Product = product24 }; var specialOffer35 = new SpecialOffer { Id = 35, ProductId = 24, Product = product24 }; product22.SpecialOffers.Add(specialOffer31); product22.SpecialOffers.Add(specialOffer32); product24.SpecialOffers.Add(specialOffer33); product24.SpecialOffers.Add(specialOffer34); product24.SpecialOffers.Add(specialOffer35); context.Add(category11); AssertAllFixedUp(context); context.Add(category12); AssertAllFixedUp(context); context.Add(category13); AssertAllFixedUp(context); context.Add(product21); AssertAllFixedUp(context); context.Add(product22); AssertAllFixedUp(context); context.Add(product23); AssertAllFixedUp(context); context.Add(product24); AssertAllFixedUp(context); context.Add(product25); AssertAllFixedUp(context); context.Add(specialOffer31); AssertAllFixedUp(context); context.Add(specialOffer32); AssertAllFixedUp(context); context.Add(specialOffer33); AssertAllFixedUp(context); context.Add(specialOffer34); AssertAllFixedUp(context); context.Add(specialOffer35); AssertAllFixedUp(context); Assert.Equal(3, category11.Products.Count); Assert.Equal(2, category12.Products.Count); Assert.Equal(0, category13.Products.Count); Assert.Equal(0, product21.SpecialOffers.Count); Assert.Equal(2, product22.SpecialOffers.Count); Assert.Equal(0, product23.SpecialOffers.Count); Assert.Equal(3, product24.SpecialOffers.Count); Assert.Equal(0, product25.SpecialOffers.Count); Assert.Equal(3, context.ChangeTracker.Entries<Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<SpecialOffer>().Count()); } }
public void Add_principal_then_dependent_one_to_one_dep_uni_FK_set_dependent_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new ParentDN { Id = 77 }; var dependent = new ChildDN { Id = 78, Parent = principal }; context.Entry(dependent).Property("ParentId").CurrentValue = principal.Id; context.Entry(principal).State = entityState; context.Entry(dependent).State = entityState; AssertFixup( context, () => { Assert.Equal(principal.Id, context.Entry(dependent).Property("ParentId").CurrentValue); Assert.Same(principal, dependent.Parent); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); }); } }
public void Add_principal_then_dependent_one_to_many_no_navs_FK_set_no_navs_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new CategoryNN { Id = 77 }; var dependent = new ProductNN { Id = 78, CategoryId = principal.Id }; context.Entry(principal).State = entityState; context.Entry(dependent).State = entityState; Assert.Equal(principal.Id, dependent.CategoryId); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); } }
public void Add_principal_but_not_dependent_one_to_many_dep_uni_FK_not_set_dependent_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new CategoryDN { Id = 77 }; var dependent = new ProductDN { Id = 78 }; context.Entry(principal).State = entityState; dependent.Category = principal; context.ChangeTracker.DetectChanges(); AssertFixup( context, () => { Assert.Equal(0, dependent.CategoryId); Assert.Same(principal, dependent.Category); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(EntityState.Detached, context.Entry(dependent).State); }); } }
public void Navigation_fixup_happens_when_entities_are_tracked_from_query() { using (var context = new FixupContext()) { var categoryType = context.Model.FindEntityType(typeof(Category)); var productType = context.Model.FindEntityType(typeof(Product)); var offerType = context.Model.FindEntityType(typeof(SpecialOffer)); var stateManager = context.ChangeTracker.GetInfrastructure(); stateManager.BeginTrackingQuery(); stateManager.StartTrackingFromQuery(categoryType, new Category { Id = 11 }, new ValueBuffer(new object[] { 11 })); stateManager.StartTrackingFromQuery(categoryType, new Category { Id = 12 }, new ValueBuffer(new object[] { 12 })); stateManager.StartTrackingFromQuery(categoryType, new Category { Id = 13 }, new ValueBuffer(new object[] { 13 })); stateManager.BeginTrackingQuery(); stateManager.StartTrackingFromQuery(productType, new Product { Id = 21, CategoryId = 11 }, new ValueBuffer(new object[] { 21, 11 })); AssertAllFixedUp(context); stateManager.StartTrackingFromQuery(productType, new Product { Id = 22, CategoryId = 11 }, new ValueBuffer(new object[] { 22, 11 })); AssertAllFixedUp(context); stateManager.StartTrackingFromQuery(productType, new Product { Id = 23, CategoryId = 11 }, new ValueBuffer(new object[] { 23, 11 })); AssertAllFixedUp(context); stateManager.StartTrackingFromQuery(productType, new Product { Id = 24, CategoryId = 12 }, new ValueBuffer(new object[] { 24, 12 })); AssertAllFixedUp(context); stateManager.StartTrackingFromQuery(productType, new Product { Id = 25, CategoryId = 12 }, new ValueBuffer(new object[] { 25, 12 })); AssertAllFixedUp(context); stateManager.BeginTrackingQuery(); stateManager.StartTrackingFromQuery(offerType, new SpecialOffer { Id = 31, ProductId = 22 }, new ValueBuffer(new object[] { 31, 22 })); AssertAllFixedUp(context); stateManager.StartTrackingFromQuery(offerType, new SpecialOffer { Id = 32, ProductId = 22 }, new ValueBuffer(new object[] { 32, 22 })); AssertAllFixedUp(context); stateManager.StartTrackingFromQuery(offerType, new SpecialOffer { Id = 33, ProductId = 24 }, new ValueBuffer(new object[] { 33, 24 })); AssertAllFixedUp(context); stateManager.StartTrackingFromQuery(offerType, new SpecialOffer { Id = 34, ProductId = 24 }, new ValueBuffer(new object[] { 34, 24 })); AssertAllFixedUp(context); stateManager.StartTrackingFromQuery(offerType, new SpecialOffer { Id = 35, ProductId = 24 }, new ValueBuffer(new object[] { 35, 24 })); AssertAllFixedUp(context); Assert.Equal(3, context.ChangeTracker.Entries<Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries<SpecialOffer>().Count()); } }
public void Navigation_fixup_is_non_destructive_to_existing_graphs() { using (var context = new FixupContext()) { var category11 = new Category { Id = 11 }; var category12 = new Category { Id = 12 }; var category13 = new Category { Id = 13 }; var product21 = new Product { Id = 21, CategoryId = 11, Category = category11 }; var product22 = new Product { Id = 22, CategoryId = 11, Category = category11 }; var product23 = new Product { Id = 23, CategoryId = 11, Category = category11 }; var product24 = new Product { Id = 24, CategoryId = 12, Category = category12 }; var product25 = new Product { Id = 25, CategoryId = 12, Category = category12 }; category11.Products.Add(product21); category11.Products.Add(product22); category11.Products.Add(product23); category12.Products.Add(product24); category12.Products.Add(product25); var specialOffer31 = new SpecialOffer { Id = 31, ProductId = 22, Product = product22 }; var specialOffer32 = new SpecialOffer { Id = 32, ProductId = 22, Product = product22 }; var specialOffer33 = new SpecialOffer { Id = 33, ProductId = 24, Product = product24 }; var specialOffer34 = new SpecialOffer { Id = 34, ProductId = 24, Product = product24 }; var specialOffer35 = new SpecialOffer { Id = 35, ProductId = 24, Product = product24 }; product22.SpecialOffers.Add(specialOffer31); product22.SpecialOffers.Add(specialOffer32); product24.SpecialOffers.Add(specialOffer33); product24.SpecialOffers.Add(specialOffer34); product24.SpecialOffers.Add(specialOffer35); context.Add(category11); AssertAllFixedUp(context); context.Add(category12); AssertAllFixedUp(context); context.Add(category13); AssertAllFixedUp(context); context.Add(product21); AssertAllFixedUp(context); context.Add(product22); AssertAllFixedUp(context); context.Add(product23); AssertAllFixedUp(context); context.Add(product24); AssertAllFixedUp(context); context.Add(product25); AssertAllFixedUp(context); context.Add(specialOffer31); AssertAllFixedUp(context); context.Add(specialOffer32); AssertAllFixedUp(context); context.Add(specialOffer33); AssertAllFixedUp(context); context.Add(specialOffer34); AssertAllFixedUp(context); context.Add(specialOffer35); AssertAllFixedUp(context); Assert.Equal(3, category11.Products.Count); Assert.Equal(2, category12.Products.Count); Assert.Equal(0, category13.Products.Count); Assert.Equal(0, product21.SpecialOffers.Count); Assert.Equal(2, product22.SpecialOffers.Count); Assert.Equal(0, product23.SpecialOffers.Count); Assert.Equal(3, product24.SpecialOffers.Count); Assert.Equal(0, product25.SpecialOffers.Count); Assert.Equal(3, context.ChangeTracker.Entries <Category>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <Product>().Count()); Assert.Equal(5, context.ChangeTracker.Entries <SpecialOffer>().Count()); } }
public void Add_principal_but_not_dependent_one_to_one_no_navs_FK_set_no_navs_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new ParentNN { Id = 77 }; var dependent = new ChildNN { Id = 78 }; context.Entry(principal).State = entityState; dependent.ParentId = principal.Id; context.ChangeTracker.DetectChanges(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.ParentId); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(EntityState.Detached, context.Entry(dependent).State); }); } }
public void Add_dependent_but_not_principal_one_to_many_FK_not_set_dependent_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new Category { Id = 77 }; var dependent = new Product { Id = 78 }; context.Entry(dependent).State = entityState; dependent.Category = principal; context.ChangeTracker.DetectChanges(); AssertFixup( context, () => { Assert.Equal(principal.Id, dependent.CategoryId); Assert.Same(principal, dependent.Category); Assert.Equal(new[] { dependent }.ToList(), principal.Products); Assert.Equal(EntityState.Added, context.Entry(principal).State); Assert.Equal(entityState == EntityState.Added ? EntityState.Added : EntityState.Modified, context.Entry(dependent).State); }); } }
public void Add_dependent_then_principal_one_to_one_prin_uni_FK_set_principal_nav_set(EntityState entityState) { using (var context = new FixupContext()) { var principal = new ParentPN { Id = 77 }; var dependent = new ChildPN { Id = 78, ParentId = principal.Id }; principal.Child = dependent; context.Entry(dependent).State = entityState; context.Entry(principal).State = entityState; Assert.Equal(principal.Id, dependent.ParentId); Assert.Same(dependent, principal.Child); Assert.Equal(entityState, context.Entry(principal).State); Assert.Equal(entityState, context.Entry(dependent).State); } }