Exemplo n.º 1
0
        public static void DropTables(this DbContext context)
        {
            var tables = context.GetCurrentTables();

            if (tables.Any())
            {
                //DROP TABLE testing1,testing2,testing3; //retry 3 times

                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        var connection = context.Database.GetDbConnection();

                        bool isConnectionClosed = connection.State == ConnectionState.Closed;

                        if (isConnectionClosed)
                        {
                            connection.Open();
                        }

                        using (var command = connection.CreateCommand())
                        {
                            command.CommandText = $"DROP TABLE {string.Join(",", tables)}";
                            command.ExecuteNonQuery();
                        }
                        break;
                    }
                    catch (Exception)
                    {
                        // Ignore
                    }
                }
            }
        }