简体   繁体   中英

MySQL Entity Framework: Can't create index

I'm using MySql.Data.Entity version 6.10.8 with Entity Framework. I'm doing "Code first" to let the MySql-provider create the database structure. Whenever the migrations contains a creation of a Index my migration fails when running the update-database command. The error message and stack trace are as follows:

System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Convert.ToDouble(String value)
at MySql.Data.Entity.MySqlMigrationSqlGenerator.Generate(CreateIndexOperation op) at MySql.Data.Entity.MySqlMigrationSqlGenerator.<.ctor>b__22_4(MigrationOperation> op) at MySql.Data.Entity.MySqlMigrationSqlGenerator.Generate(IEnumerable 1 migrationOperations, String providerManifestToken) at System.Data.Entity.Migrations.DbMigrator.GenerateStatements(IList 1 operations, String migrationId) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.GenerateStatements(IList`1 operations, String migrationId) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable 1 operations, IEnumerable 1 systemOperations, Boolean downgrading, Boolean auto)
at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable 1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable 1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClasse.b__d() at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration) at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() Input string was not in a correct format.

How to repeat:
Use Swedish Windows (or any other language which do not use "." as the decimal separator).

Create a migration file with an index, such as:

CreateTable(
"dbo.AspNetRoles",
c => new
{
  Id = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
  Name = c.String(nullable: false, maxLength: 256, storeType: "nvarchar"),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex"); // This line causes the exception

Run database-update

(A bug report has been sent to MySql: https://bugs.mysql.com/bug.php?id=92561 )

This error is due to MySqlMigrationSqlGenerator.Generate(CreateIndexOperation op) which checks the version of the database by converting a string to double. However it does so without specifying an IFormatProvider. Since Swedish use "," as the decimal separator this conversion fails (the version number is separated by ".").

By overriding MySqlMigrationSqlGenerator.Generate(CreateIndexOperation op) this can be avoided. Use the code from this answer https://stackoverflow.com/a/51756143/1037864 (which is an answer for a different question)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM