Entity framework with mysql database migrations fail, when creating indexes(创建索引时,具有 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 数据库迁移的实体框架失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:创建索引时,具有 mysql 数据库迁移的实体框架失败
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01