问题描述
是什么导致 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 数据库迁移的实体框架失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!