Entity framework with mysql database migrations fail, when creating indexes

后端 未结 3 1673
借酒劲吻你
借酒劲吻你 2020-12-19 16:11

what causes this error in MySQL with entity framework? I can generate the migration script and connect to the database but it doesn\'t like the SQL generated particularly \"

3条回答
  •  轮回少年
    2020-12-19 16:59

    i haved the same problem, after i read on posts, i decided create a class inherits ofMySqlMigrationSqlGenerator and override protected override MigrationStatement Generate ( CreateIndexOperation op ), then on configuration of migration i add : SetSqlGenerator ( "MySql.Data.MySqlClient", new myMigrationSQLGenerator ( ) );

    this is the code of class:

    public class myMigrationSQLGenerator : MySqlMigrationSqlGenerator
    {
        private string TrimSchemaPrefix ( string table )
        {
            if ( table.StartsWith ( "dbo." ) )
                return table.Replace ( "dbo.", "" );
            return table;
        }
    
        protected override MigrationStatement Generate ( CreateIndexOperation op )
        {
            var u = new MigrationStatement ( );
            string unique = ( op.IsUnique ? "UNIQUE" : "" ), columns = "";
            foreach ( var col in op.Columns )
            {
                columns += ( $"`{col}` DESC{( op.Columns.IndexOf ( col ) < op.Columns.Count - 1 ? ", " : "" )}" );
            }
            u.Sql = $"CREATE {unique} INDEX `{op.Name}` ON `{TrimSchemaPrefix ( op.Table )}` ({columns}) USING BTREE";
            return u;
        }
    }
    

    and this is the code on Migrations\Configuration.cs:

        public Configuration ()
        {           
            AutomaticMigrationsEnabled = false;
            SetSqlGenerator ( "MySql.Data.MySqlClient", new myMigrationSQLGenerator ( ) );
        }
    

    this work for me.

提交回复
热议问题