Exemple #1
0
        public static void RunMigration(this DbContext context, DbMigration migration)
        {
            var prop = migration.GetType().GetProperty("Operations", BindingFlags.NonPublic | BindingFlags.Instance);

            if (prop != null)
            {
                IEnumerable <MigrationOperation> operations = prop.GetValue(migration) as IEnumerable <MigrationOperation>;
                var generator  = new MySqlMigrationSqlGenerator();
                var statements = generator.Generate(operations, "2008");
                foreach (MigrationStatement item in statements)
                {
                    context.Database.ExecuteSqlCommand(item.Sql);
                }
            }
        }
        /// <summary>
        /// Generate and apply sql statemens from the
        /// migration operations list
        /// return false is case of fail or if database doesn't exist
        /// </summary>
        private bool GenerateAndExecuteMySQLStatements(List <MigrationOperation> migrationOperations)
        {
            MySqlProviderServices ProviderServices;

            ProviderServices = new MySqlProviderServices();

            using (BlogContext context = new BlogContext())
            {
                if (!context.Database.Exists())
                {
                    return(false);
                }

                using (MySqlConnection conn = new MySqlConnection(context.Database.Connection.ConnectionString))
                {
                    var migratorGenerator = new MySqlMigrationSqlGenerator();
                    var Token             = ProviderServices.GetProviderManifestToken(conn);
                    var sqlStmts          = migratorGenerator.Generate(migrationOperations, providerManifestToken: Token);
                    if (conn.State == System.Data.ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    foreach (MigrationStatement stmt in sqlStmts)
                    {
                        try
                        {
                            MySqlCommand cmd = new MySqlCommand(stmt.Sql, conn);
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }