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 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 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 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 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); } }
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); }); } }
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 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); }); }
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 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 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 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); } }
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_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_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 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 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_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_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 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 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_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); } }
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); }); } }
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_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_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_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); }); } }