コード例 #1
0
        public void Test1()
        {
            StoreContext _context = new StoreContextFactory().CreateDbContext(null);
            var          cat      = new Category {
                CategoryName = "CatName"
            };

            _context.Categories.Add(cat);
            _context.SaveChanges();


            var cats = new List <Category>
            {
                new Category {
                    CategoryName = "Cat1Name"
                },
                new Category {
                    CategoryName = "Cat2Name"
                },
                new Category {
                    CategoryName = "Cat3Name"
                },
            };

            _context.Categories.AddRange(cats);
            _context.SaveChanges();
        }
        public void ShouldDeleteACategoryWithTimestampData()
        {
            var category = new Category {
                CategoryName = "Foo"
            };

            _db.Categories.Add(category);
            _db.SaveChanges();
            var context     = new StoreContextFactory().CreateDbContext(null);
            var catToDelete = new Category {
                Id = category.Id, TimeStamp = category.TimeStamp
            };

            context.Entry(catToDelete).State = EntityState.Deleted;
            var affected = context.SaveChanges();

            Assert.Equal(1, affected);
        }
コード例 #3
0
        public void ShouldNotDeleteACategoryWithoutTimestampData()
        {
            var category = new Category {
                CategoryName = "Foo"
            };

            _db.Categories.Add(category);
            _db.SaveChanges();
            var context     = new StoreContextFactory().CreateDbContext(null);
            var catToDelete = new Category {
                Id = category.Id
            };

            context.Categories.Remove(catToDelete);
            var ex = Assert.Throws <DbUpdateConcurrencyException>(() => context.SaveChanges());

            Assert.Equal(1, ex.Entries.Count);
            Assert.Equal(category.Id, ((Category)ex.Entries[0].Entity).Id);
        }