创建索引时,具有 mysql 数据库迁移的实体框架失败

Entity framework with mysql database migrations fail, when creating indexes(创建索引时,具有 mysql 数据库迁移的实体框架失败)
本文介绍了创建索引时,具有 mysql 数据库迁移的实体框架失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么导致 MySQL 与实体框架出现此错误?我可以生成迁移脚本并连接到数据库,但它不喜欢在尝试创建索引时生成的特别散列"的 SQL.

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 "hash" when trying to create indexes.

例子:

CREATE index  `IX_Facility_ID` on `Contact.Address` (`Facility_ID` DESC) using HASH

错误:

MySql.Data.MySqlClient.MySqlException (0x80004005):空间/全文/哈希索引和显式索引顺序的使用不正确

MySql.Data.MySqlClient.MySqlException (0x80004005): Incorrect usage of spatial/fulltext/hash index and explicit index order

有没有办法解决这个问题?这是 EF 6 和最新的 mysql dll.

Is there any way around this? This is with EF 6 and the latest mysql dlls.

推荐答案

我也遇到了同样的问题,看了帖子后,我决定创建一个类继承MySqlMigrationSqlGenerator 并覆盖protected override MigrationStatement Generate ( CreateIndexOperationop ),然后在配置迁移时添加:SetSqlGenerator ("MySql.Data.MySqlClient", new myMigrationSQLGenerator ());

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 ( ) );

这是类的代码:

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;
    }
}

这是 MigrationsConfiguration.cs 上的代码:

and this is the code on MigrationsConfiguration.cs:

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

这对我有用.

这篇关于创建索引时,具有 mysql 数据库迁移的实体框架失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)