示例#1
0
        public long CallSaveChangesOnceInTransaction()
        {
            _timer.Restart();
            try
            {
                using (var dbContext = new ApplicationDbContext())
                {
                    using (var transactionScope = dbContext.Database.BeginTransaction())
                    {
                        for (var i = 0; i < _iterations; i++)
                        {
                            dbContext.Products.Add(new Product(Guid.NewGuid()));
                        }

                        dbContext.SaveChanges();
                        transactionScope.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return _timer.ElapsedMilliseconds;
        }
示例#2
0
        public long BulkInsert()
        {
            _timer.Restart();
            using (var dbContext = new ApplicationDbContext())
            {
                var products = new List<Product>();
                using (var transactionScope = dbContext.Database.BeginTransaction())
                {
                    for (var i = 0; i < _iterations; i++)
                    {
                        products.Add(new Product(Guid.NewGuid()));
                    }
                    dbContext.BulkInsert(products);

                    dbContext.SaveChanges();
                    transactionScope.Commit();
                }
            }
            return _timer.ElapsedMilliseconds;
        }
示例#3
0
        public long CallSaveChangesOnceUsingAddRange()
        {
            _timer.Restart();
            using (var dbContext = new ApplicationDbContext())
            {
                var products = new List<Product>();
                for (var i = 0; i < _iterations; i++)
                {
                    products.Add(new Product(Guid.NewGuid()));

                }
                dbContext.Products.AddRange(products);
                dbContext.SaveChanges();
            }
            return _timer.ElapsedMilliseconds;
        }
示例#4
0
 public long WarmingUp()
 {
     _timer.Restart();
     using (var dbContext = new ApplicationDbContext())
     {
         dbContext.Products.Add(new Product(Guid.NewGuid()));
         dbContext.SaveChanges();
     }
     return _timer.ElapsedMilliseconds;
 }
示例#5
0
 public long CallSaveChangesRepeatedly()
 {
     _timer.Restart();
     using (var dbContext = new ApplicationDbContext())
     {
         for (var i = 0; i < _iterations; i++)
         {
             dbContext.Products.Add(new Product(Guid.NewGuid()));
             dbContext.SaveChanges();
         }
     }
     return _timer.ElapsedMilliseconds;
 }