Пример #1
0
        public void Auto_DetectChanges_for_Entry_can_be_switched_off(bool useGenericOverload)
        {
            using (var context = new ButTheHedgehogContext(InMemoryTestHelpers.Instance.CreateServiceProvider()))
            {
                context.ChangeTracker.AutoDetectChangesEnabled = false;

                var entry = context.Attach(new Product {
                    Id = 1, Name = "Little Hedgehogs"
                });

                entry.Entity.Name = "Cracked Cookies";

                Assert.Equal(EntityState.Unchanged, entry.State);

                if (useGenericOverload)
                {
                    context.Entry(entry.Entity);
                }
                else
                {
                    context.Entry((object)entry.Entity);
                }

                Assert.Equal(EntityState.Unchanged, entry.State);
            }
        }
Пример #2
0
        public void Entry_calls_DetectChanges_by_default(bool useGenericOverload)
        {
            using (var context = new ButTheHedgehogContext(InMemoryTestHelpers.Instance.CreateServiceProvider()))
            {
                var entry = context.Attach(
                    new Product
                {
                    Id   = 1,
                    Name = "Little Hedgehogs"
                });

                entry.Entity.Name = "Cracked Cookies";

                Assert.Equal(EntityState.Unchanged, entry.State);

                if (useGenericOverload)
                {
                    context.Entry(entry.Entity);
                }
                else
                {
                    context.Entry((object)entry.Entity);
                }

                Assert.Equal(EntityState.Modified, entry.State);
            }
        }
Пример #3
0
        public void Local_calls_DetectChanges()
        {
            var provider =
                InMemoryTestHelpers.Instance.CreateServiceProvider(
                    new ServiceCollection().AddScoped <IChangeDetector, ChangeDetectorProxy>());

            using (var context = new ButTheHedgehogContext(provider))
            {
                var changeDetector = (ChangeDetectorProxy)context.GetService <IChangeDetector>();

                changeDetector.DetectChangesCalled = false;

                var entry = context.Attach(
                    new Product
                {
                    Id   = 1,
                    Name = "Little Hedgehogs"
                });

                entry.Entity.Name = "Big Hedgehogs";

                Assert.False(changeDetector.DetectChangesCalled);

                var _ = context.Set <Product>().Local;

                Assert.True(changeDetector.DetectChangesCalled);
                Assert.Equal(EntityState.Modified, entry.State);
            }
        }
Пример #4
0
        public void Add_Attach_Remove_Update_do_not_call_DetectChanges()
        {
            var provider = TestHelpers.Instance.CreateServiceProvider(new ServiceCollection().AddScoped<IChangeDetector, ChangeDetectorProxy>());
            using (var context = new ButTheHedgehogContext(provider))
            {
                var changeDetector = (ChangeDetectorProxy)context.GetService<IChangeDetector>();

                var id = 1;

                changeDetector.DetectChangesCalled = false;

                context.Add(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.Add((object)new Product { Id = id++, Name = "Little Hedgehogs" });
                context.AddRange(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.AddRange(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.AddRange(new List<Product> { new Product { Id = id++, Name = "Little Hedgehogs" } });
                context.AddRange(new List<object> { new Product { Id = id++, Name = "Little Hedgehogs" } });
                context.Attach(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.Attach((object)new Product { Id = id++, Name = "Little Hedgehogs" });
                context.AttachRange(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.AttachRange(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.AttachRange(new List<Product> { new Product { Id = id++, Name = "Little Hedgehogs" } });
                context.AttachRange(new List<object> { new Product { Id = id++, Name = "Little Hedgehogs" } });
                context.Update(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.Update((object)new Product { Id = id++, Name = "Little Hedgehogs" });
                context.UpdateRange(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.UpdateRange(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.UpdateRange(new List<Product> { new Product { Id = id++, Name = "Little Hedgehogs" } });
                context.UpdateRange(new List<object> { new Product { Id = id++, Name = "Little Hedgehogs" } });
                context.Remove(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.Remove((object)new Product { Id = id++, Name = "Little Hedgehogs" });
                context.RemoveRange(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.RemoveRange(new Product { Id = id++, Name = "Little Hedgehogs" });
                context.RemoveRange(new List<Product> { new Product { Id = id++, Name = "Little Hedgehogs" } });
                context.RemoveRange(new List<object> { new Product { Id = id++, Name = "Little Hedgehogs" } });
                context.Add(new Product { Id = id++, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject);
                context.Add((object)new Product { Id = id++, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject);
                context.AddRange(new List<Product> { new Product { Id = id++, Name = "Little Hedgehogs" } }, behavior: GraphBehavior.SingleObject);
                context.AddRange(new List<object> { new Product { Id = id++, Name = "Little Hedgehogs" } }, behavior: GraphBehavior.SingleObject);
                context.Attach(new Product { Id = id++, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject);
                context.Attach((object)new Product { Id = id++, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject);
                context.AttachRange(new List<Product> { new Product { Id = id++, Name = "Little Hedgehogs" } }, behavior: GraphBehavior.SingleObject);
                context.AttachRange(new List<object> { new Product { Id = id++, Name = "Little Hedgehogs" } }, behavior: GraphBehavior.SingleObject);
                context.Update(new Product { Id = id++, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject);
                context.Update((object)new Product { Id = id++, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject);
                context.UpdateRange(new List<Product> { new Product { Id = id++, Name = "Little Hedgehogs" } }, behavior: GraphBehavior.SingleObject);
                context.UpdateRange(new List<object> { new Product { Id = id++, Name = "Little Hedgehogs" } }, behavior: GraphBehavior.SingleObject);

                Assert.False(changeDetector.DetectChangesCalled);

                context.ChangeTracker.DetectChanges();

                Assert.True(changeDetector.DetectChangesCalled);
            }
        }
Пример #5
0
        public void Auto_DetectChanges_for_Entry_can_be_switched_off(bool useGenericOverload)
        {
            using (var context = new ButTheHedgehogContext(TestHelpers.Instance.CreateServiceProvider()))
            {
                context.ChangeTracker.AutoDetectChangesEnabled = false;

                var entry = context.Attach(new Product { Id = 1, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject);

                entry.Entity.Name = "Cracked Cookies";

                Assert.Equal(EntityState.Unchanged, entry.State);

                if (useGenericOverload)
                {
                    context.Entry(entry.Entity);
                }
                else
                {
                    context.Entry((object)entry.Entity);
                }

                Assert.Equal(EntityState.Unchanged, entry.State);
            }
        }
Пример #6
0
        public void Entry_calls_DetectChanges_by_default(bool useGenericOverload)
        {
            using (var context = new ButTheHedgehogContext(TestHelpers.Instance.CreateServiceProvider()))
            {
                var entry = context.Attach(new Product { Id = 1, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject);

                entry.Entity.Name = "Cracked Cookies";

                Assert.Equal(EntityState.Unchanged, entry.State);

                if (useGenericOverload)
                {
                    context.Entry(entry.Entity);
                }
                else
                {
                    context.Entry((object)entry.Entity);
                }

                Assert.Equal(EntityState.Modified, entry.State);
            }
        }
Пример #7
0
        public async Task Auto_DetectChanges_for_SaveChanges_can_be_switched_off(bool async)
        {
            var provider = TestHelpers.Instance.CreateServiceProvider();

            using (var context = new ButTheHedgehogContext(provider))
            {
                context.ChangeTracker.AutoDetectChangesEnabled = false;
                Assert.False(context.ChangeTracker.AutoDetectChangesEnabled);

                var product = context.Attach(new Product { Id = 1, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject).Entity;

                product.Name = "Cracked Cookies";

                if (async)
                {
                    await context.SaveChangesAsync();
                }
                else
                {
                    context.SaveChanges();
                }
            }

            using (var context = new ButTheHedgehogContext(provider))
            {
                Assert.Empty(context.Products);
            }
        }
Пример #8
0
        public async Task SaveChanges_calls_DetectChanges_by_default(bool async)
        {
            var provider = TestHelpers.Instance.CreateServiceProvider();

            using (var context = new ButTheHedgehogContext(provider))
            {
                Assert.True(context.ChangeTracker.AutoDetectChangesEnabled);

                var product = context.Attach(new Product { Id = 1, Name = "Little Hedgehogs" }, behavior: GraphBehavior.SingleObject).Entity;

                product.Name = "Cracked Cookies";

                if (async)
                {
                    await context.SaveChangesAsync();
                }
                else
                {
                    context.SaveChanges();
                }
            }

            using (var context = new ButTheHedgehogContext(provider))
            {
                Assert.Equal("Cracked Cookies", context.Products.Single().Name);
            }
        }
Пример #9
0
        public void Add_Attach_Remove_Update_do_not_call_DetectChanges()
        {
            var provider = TestHelpers.Instance.CreateServiceProvider(new ServiceCollection().AddScoped<IChangeDetector, ChangeDetectorProxy>());
            using (var context = new ButTheHedgehogContext(provider))
            {
                var changeDetector = (ChangeDetectorProxy)((IAccessor<IServiceProvider>)context).Service
                    .GetRequiredService<IChangeDetector>();

                var entity = new Product { Id = 1, Name = "Little Hedgehogs" };

                changeDetector.DetectChangesCalled = false;

                context.Add(entity);
                context.Add((object)entity);
                context.AddRange(entity);
                context.AddRange(entity);
                context.AddRange(new List<Product> { entity });
                context.AddRange(new List<object> { entity });
                context.Attach(entity);
                context.Attach((object)entity);
                context.AttachRange(entity);
                context.AttachRange(entity);
                context.AttachRange(new List<Product> { entity });
                context.AttachRange(new List<object> { entity });
                context.Update(entity);
                context.Update((object)entity);
                context.UpdateRange(entity);
                context.UpdateRange(entity);
                context.UpdateRange(new List<Product> { entity });
                context.UpdateRange(new List<object> { entity });
                context.Remove(entity);
                context.Remove((object)entity);
                context.RemoveRange(entity);
                context.RemoveRange(entity);
                context.RemoveRange(new List<Product> { entity });
                context.RemoveRange(new List<object> { entity });

                Assert.False(changeDetector.DetectChangesCalled);

                context.ChangeTracker.DetectChanges();

                Assert.True(changeDetector.DetectChangesCalled);
            }
        }
Пример #10
0
        public async Task Add_Attach_Remove_Update_do_not_call_DetectChanges()
        {
            var provider = InMemoryTestHelpers.Instance.CreateServiceProvider(new ServiceCollection().AddScoped <IChangeDetector, ChangeDetectorProxy>());

            using (var context = new ButTheHedgehogContext(provider))
            {
                var changeDetector = (ChangeDetectorProxy)context.GetService <IChangeDetector>();

                var id = 1;

                changeDetector.DetectChangesCalled = false;

                context.Add(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.Add((object)new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.AddRange(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.AddRange(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.AddRange(new List <Product> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });
                context.AddRange(new List <object> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });
                await context.AddAsync(new Product { Id = id++, Name = "Little Hedgehogs" });

                await context.AddAsync((object)new Product { Id = id++, Name = "Little Hedgehogs" });

                await context.AddRangeAsync(new Product { Id = id++, Name = "Little Hedgehogs" });

                await context.AddRangeAsync(new Product { Id = id++, Name = "Little Hedgehogs" });

                await context.AddRangeAsync(new List <Product> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });

                await context.AddRangeAsync(new List <object> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });

                context.Attach(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.Attach((object)new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.AttachRange(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.AttachRange(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.AttachRange(new List <Product> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });
                context.AttachRange(new List <object> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });
                context.Update(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.Update((object)new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.UpdateRange(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.UpdateRange(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.UpdateRange(new List <Product> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });
                context.UpdateRange(new List <object> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });
                context.Remove(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.Remove((object)new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.RemoveRange(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.RemoveRange(new Product {
                    Id = id++, Name = "Little Hedgehogs"
                });
                context.RemoveRange(new List <Product> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });
                context.RemoveRange(new List <object> {
                    new Product {
                        Id = id++, Name = "Little Hedgehogs"
                    }
                });

                Assert.False(changeDetector.DetectChangesCalled);

                context.ChangeTracker.DetectChanges();

                Assert.True(changeDetector.DetectChangesCalled);
            }
        }