Ejemplo n.º 1
0
        public static int Truncates(this dbContext db, params string[] tables)
        {
            List <string> target = new List <string>();
            int           result = 0;

            if (tables == null || tables.Length == 0)
            {
                target = db.GetTableList();
            }
            else
            {
                target.AddRange(tables);
            }

            using (TransactionScope scope = new TransactionScope())
            {
                foreach (var table in target)
                {
                    db.db.Database.ExecuteSqlCommand("SET FOREIGN_KEY_CHECKS=0");
                    result += db.db.Database.ExecuteSqlCommand(string.Format("DELETE FROM  {0}", table));
                    db.db.Database.ExecuteSqlCommand(string.Format("ALTER TABLE {0} AUTO_INCREMENT = 1", table));
                    db.db.Database.ExecuteSqlCommand("SET FOREIGN_KEY_CHECKS=1");
                }

                scope.Complete();
            }

            return(result);
        }