创建索引时,带有mysql数据库迁移的实体框架失败 [英] Entity framework with mysql database migrations fail, when creating indexes
问题描述
是什么导致带有实体框架的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的类并覆盖受 保护的覆盖MigrationStatement Generate(CreateIndexOperation op) ,然后在配置迁移时添加: SetSqlGenerator("MySql.Data.MySqlClient",新的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;
}
}
这是 Migrations \ Configuration.cs 上的代码:
and this is the code on Migrations\Configuration.cs:
public Configuration ()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator ( "MySql.Data.MySqlClient", new myMigrationSQLGenerator ( ) );
}
这对我有用.
这篇关于创建索引时,带有mysql数据库迁移的实体框架失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!