예제 #1
0
        public void Thirty()
        {
            var db = new DbContextOptionsBuilder();

            db.UseInMemoryDatabase();
            BatchDeleteManager.InMemoryDbContextFactory = () => new TestContextMemory(db.Options);

            using (var ctx = new TestContextMemory(db.Options))
            {
                TestContext.DeleteAll(ctx, x => x.Entity_Basics);
                TestContext.Insert(ctx, x => x.Entity_Basics, 50);
            }

            using (var ctx = new TestContextMemory(db.Options))
            {
                // BEFORE
                Assert.AreEqual(1225, ctx.Entity_Basics.Sum(x => x.ColumnInt));

                // ACTION
                var rowsAffected = ctx.Entity_Basics.Where(x => x.ColumnInt > 10 && x.ColumnInt <= 40).Delete();

                // AFTER
                Assert.AreEqual(460, ctx.Entity_Basics.Sum(x => x.ColumnInt));
                Assert.AreEqual(30, rowsAffected);
            }
        }
예제 #2
0
        public static void DeleteAll <T>(TestContextMemory ctx, Func <TestContextMemory, DbSet <T> > func) where T : class
        {
            var sets = func(ctx);

            sets.RemoveRange(sets);
            ctx.SaveChanges();

            Assert.AreEqual(0, sets.Count());
        }
예제 #3
0
        public static List <T> Insert <T>(TestContextMemory ctx, Func <TestContextMemory, DbSet <T> > func, int count) where T : class, new()
        {
            var sets = func(ctx);

            var countBefore = sets.Count();

            var list = new List <T>();

            for (var i = 0; i < count; i++)
            {
                var item = new T();
                InsertFactory(item, i);
                list.Add(item);
            }

            sets.AddRange(list);
            ctx.SaveChanges();

            Assert.AreEqual(count + countBefore, sets.Count());

            return(list);
        }