public void Auto_DetectChanges_for_Entries_can_be_switched_off(bool useGenericOverload) { using (var context = new EarlyLearningCenter()) { context.ChangeTracker.AutoDetectChangesEnabled = false; var entry = context.Attach(new Product { Id = 1, CategoryId = 66 }); entry.Entity.CategoryId = 77; Assert.Equal(EntityState.Unchanged, entry.State); if (useGenericOverload) { context.ChangeTracker.Entries <Product>(); } else { context.ChangeTracker.Entries(); } Assert.Equal(EntityState.Unchanged, entry.State); } }
public async Task Explicitly_calling_DetectChanges_works_even_if_auto_DetectChanges_is_switched_off(bool async) { using (var context = new EarlyLearningCenter()) { context.Configuration.AutoDetectChangesEnabled = false; var entry = context.Attach(new Product { Id = 1, CategoryId = 66 }); entry.Entity.CategoryId = 77; Assert.Equal(EntityState.Unchanged, entry.State); if (async) { await context.ChangeTracker.DetectChangesAsync(); } else { context.ChangeTracker.DetectChanges(); } Assert.Equal(EntityState.Modified, entry.State); } }
public void Explicitly_calling_DetectChanges_works_even_if_auto_DetectChanges_is_switched_off() { using (var context = new EarlyLearningCenter()) { context.ChangeTracker.AutoDetectChangesEnabled = false; var entry = context.Attach(new Product { Id = 1, CategoryId = 66 }); entry.Entity.CategoryId = 77; Assert.Equal(EntityState.Unchanged, entry.State); context.ChangeTracker.DetectChanges(); Assert.Equal(EntityState.Modified, entry.State); } }
public void Entities_that_are_already_tracked_will_not_get_attached() { using (var context = new EarlyLearningCenter()) { var existingProduct = context.Attach(new Product { Id = 2, CategoryId = 1 }).Entity; var category = new Category { Id = 1, Products = new List <Product> { new Product { Id = 1 }, existingProduct, new Product { Id = 3 } } }; context.ChangeTracker.TrackGraph(category, e => e.State = EntityState.Modified); Assert.Equal(4, context.ChangeTracker.Entries().Count()); Assert.Equal(EntityState.Modified, context.Entry(category).State); Assert.Equal(EntityState.Modified, context.Entry(category.Products[0]).State); Assert.Equal(EntityState.Unchanged, context.Entry(category.Products[1]).State); Assert.Equal(EntityState.Modified, context.Entry(category.Products[2]).State); Assert.Same(category, category.Products[0].Category); Assert.Same(category, category.Products[1].Category); Assert.Same(category, category.Products[2].Category); Assert.Equal(category.Id, category.Products[0].CategoryId); Assert.Equal(category.Id, category.Products[1].CategoryId); Assert.Equal(category.Id, category.Products[2].CategoryId); } }
public void Entries_calls_DetectChanges_by_default(bool useGenericOverload) { using (var context = new EarlyLearningCenter()) { var entry = context.Attach(new Product { Id = 1, CategoryId = 66 }); entry.Entity.CategoryId = 77; Assert.Equal(EntityState.Unchanged, entry.State); if (useGenericOverload) { context.ChangeTracker.Entries <Product>(); } else { context.ChangeTracker.Entries(); } Assert.Equal(EntityState.Modified, entry.State); } }
[Fact] // Issue #1246 public void Can_attach_with_inconsistent_FK_dependent_first_reference_not_fixed_up() { using (var context = new EarlyLearningCenter(TestHelpers.Instance.CreateServiceProvider())) { var category = new Category { Id = 1, Name = "Beverages" }; var product = new Product { Id = 1, CategoryId = 7, Name = "Marmite" }; category.Products = new List<Product> { product }; context.Attach(product, behavior: GraphBehavior.SingleObject); Assert.Equal(7, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Null(product.Category); Assert.Equal(EntityState.Detached, context.Entry(category).State); Assert.Equal(EntityState.Unchanged, context.Entry(product).State); context.Attach(category, behavior: GraphBehavior.SingleObject); Assert.Equal(7, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Null(product.Category); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); Assert.Equal(EntityState.Unchanged, context.Entry(product).State); } }
[Fact] // Issue #1246 public void Can_attach_with_inconsistent_FK_principal_first_fully_fixed_up() { using (var context = new EarlyLearningCenter(TestHelpers.Instance.CreateServiceProvider())) { var category = new Category { Id = 1, Name = "Beverages" }; var product = new Product { Id = 1, CategoryId = 7, Name = "Marmite", Category = category }; category.Products = new List<Product> { product }; context.Attach(category, behavior: GraphBehavior.SingleObject); Assert.Equal(7, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category, product.Category); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); Assert.Equal(EntityState.Detached, context.Entry(product).State); context.Attach(product, behavior: GraphBehavior.SingleObject); Assert.Equal(7, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category, product.Category); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); // Dependent is Unchanged here because the FK change happened before it was attached Assert.Equal(EntityState.Unchanged, context.Entry(product).State); } }
[Fact] // Issue #1246 public void Can_set_set_to_Unchanged_with_inconsistent_FK_principal_first_reference_not_fixed_up_with_tracked_FK_match() { using (var context = new EarlyLearningCenter(TestHelpers.Instance.CreateServiceProvider())) { var category7 = context.Attach(new Category { Id = 7, Products = new List<Product>() }, behavior: GraphBehavior.SingleObject).Entity; var category = new Category { Id = 1, Name = "Beverages" }; var product = new Product { Id = 1, CategoryId = 7, Name = "Marmite" }; category.Products = new List<Product> { product }; context.Entry(category).State = EntityState.Unchanged; Assert.Equal(7, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Null(product.Category); Assert.Empty(category7.Products); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); Assert.Equal(EntityState.Detached, context.Entry(product).State); context.Entry(product).State = EntityState.Unchanged; Assert.Equal(7, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category7, product.Category); Assert.Same(product, category7.Products.Single()); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); Assert.Equal(EntityState.Unchanged, context.Entry(product).State); } }
[Fact] // Issue #1246 public void Can_attach_with_inconsistent_FK_dependent_first_collection_not_fixed_up() { using (var context = new EarlyLearningCenter(TestHelpers.Instance.CreateServiceProvider())) { var category = new Category { Id = 1, Name = "Beverages" }; var product = new Product { Id = 1, CategoryId = 7, Name = "Marmite", Category = category }; category.Products = new List<Product>(); context.Attach(product); Assert.Equal(1, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category, product.Category); Assert.Equal(EntityState.Detached, context.Entry(category).State); Assert.Equal(EntityState.Modified, context.Entry(product).State); context.Attach(category); Assert.Equal(1, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category, product.Category); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); Assert.Equal(EntityState.Modified, context.Entry(product).State); } }
[Fact] // Issue #1246 public void Can_set_set_to_Unchanged_with_inconsistent_FK_dependent_first_fully_fixed_up_with_tracked_FK_match() { using (var context = new EarlyLearningCenter(TestHelpers.Instance.CreateServiceProvider())) { var category7 = context.Attach(new Category { Id = 7, Products = new List<Product>() }).Entity; var category = new Category { Id = 1, Name = "Beverages" }; var product = new Product { Id = 1, CategoryId = 7, Name = "Marmite", Category = category }; category.Products = new List<Product> { product }; context.Entry(product).State = EntityState.Unchanged; Assert.Equal(1, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category, product.Category); Assert.Empty(category7.Products); Assert.Equal(EntityState.Detached, context.Entry(category).State); Assert.Equal(EntityState.Modified, context.Entry(product).State); context.Entry(category).State = EntityState.Unchanged; Assert.Equal(1, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category, product.Category); Assert.Empty(category7.Products); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); Assert.Equal(EntityState.Modified, context.Entry(product).State); } }
[Fact] // Issue #1246 public void Can_attach_with_inconsistent_FK_principal_first_reference_not_fixed_up_with_tracked_FK_match() { using (var context = new EarlyLearningCenter(TestHelpers.Instance.CreateServiceProvider())) { var category7 = context.Attach(new Category { Id = 7, Products = new List<Product>() }).Entity; var category = new Category { Id = 1, Name = "Beverages" }; var product = new Product { Id = 1, CategoryId = 7, Name = "Marmite" }; category.Products = new List<Product> { product }; context.Attach(category); Assert.Equal(1, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category, product.Category); Assert.Empty(category7.Products); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); Assert.Equal(EntityState.Detached, context.Entry(product).State); context.Attach(product); Assert.Equal(1, product.CategoryId); Assert.Same(product, category.Products.Single()); Assert.Same(category, product.Category); Assert.Empty(category7.Products); Assert.Equal(EntityState.Unchanged, context.Entry(category).State); // Dependent is Unchanged here because the FK change happened before it was attached Assert.Equal(EntityState.Unchanged, context.Entry(product).State); } }
public void Auto_DetectChanges_for_Entries_can_be_switched_off(bool useGenericOverload) { using (var context = new EarlyLearningCenter()) { context.ChangeTracker.AutoDetectChangesEnabled = false; var entry = context.Attach(new Product { Id = 1, CategoryId = 66 }); entry.Entity.CategoryId = 77; Assert.Equal(EntityState.Unchanged, entry.State); if (useGenericOverload) { context.ChangeTracker.Entries<Product>(); } else { context.ChangeTracker.Entries(); } Assert.Equal(EntityState.Unchanged, entry.State); } }
public void Entries_calls_DetectChanges_by_default(bool useGenericOverload) { using (var context = new EarlyLearningCenter()) { var entry = context.Attach(new Product { Id = 1, CategoryId = 66 }); entry.Entity.CategoryId = 77; Assert.Equal(EntityState.Unchanged, entry.State); if (useGenericOverload) { context.ChangeTracker.Entries<Product>(); } else { context.ChangeTracker.Entries(); } Assert.Equal(EntityState.Modified, entry.State); } }
public void Entities_that_are_already_tracked_will_not_get_attached() { using (var context = new EarlyLearningCenter()) { var existingProduct = context.Attach(new Product { Id = 2, CategoryId = 1 }).Entity; var category = new Category { Id = 1, Products = new List<Product> { new Product { Id = 1 }, existingProduct, new Product { Id = 3 } } }; context.ChangeTracker.TrackGraph(category, e => e.State = EntityState.Modified); Assert.Equal(4, context.ChangeTracker.Entries().Count()); Assert.Equal(EntityState.Modified, context.Entry(category).State); Assert.Equal(EntityState.Modified, context.Entry(category.Products[0]).State); Assert.Equal(EntityState.Unchanged, context.Entry(category.Products[1]).State); Assert.Equal(EntityState.Modified, context.Entry(category.Products[2]).State); Assert.Same(category, category.Products[0].Category); Assert.Same(category, category.Products[1].Category); Assert.Same(category, category.Products[2].Category); Assert.Equal(category.Id, category.Products[0].CategoryId); Assert.Equal(category.Id, category.Products[1].CategoryId); Assert.Equal(category.Id, category.Products[2].CategoryId); } }