【问题标题】:MySQL Entity Framework: Can't create indexMySQL 实体框架:无法创建索引
【发布时间】:2019-03-01 04:02:04
【问题描述】:

我将 MySql.Data.Entity 6.10.8 版与实体框架一起使用。 我正在做“代码优先”让 MySql-provider 创建数据库结构。 每当迁移包含创建索引时,我的迁移在运行 update-database 命令时都会失败。错误信息和堆栈跟踪如下:

System.FormatException:输入字符串的格式不正确。
在 System.Number.ParseDouble(字符串值,NumberStyles 选项, NumberFormatInfo numfmt) 在 System.Convert.ToDouble(字符串值)
在 MySql.Data.Entity.MySqlMigrationSqlGenerator.Generate(CreateIndexOperation 操作)在 MySql.Data.Entity.MySqlMigrationSqlGenerator.<.ctor>b__22_4(MigrationOperation> op) 在 MySql.Data.Entity.MySqlMigrationSqlGenerator.Generate(IEnumerable 1 迁移操作,字符串 providerManifestToken)在 System.Data.Entity.Migrations.DbMigrator.GenerateStatements(IList 1 操作,字符串 migrationId) 在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.GenerateStatements(IList`1 操作,字符串 migrationId) 在 System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(字符串 migrationId, VersionedModel targetModel, IEnumerable 1 个操作, IEnumerable 1 systemOperations,布尔降级,布尔自动)
在 System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration 迁移,DbMigration lastMigration)在 System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration 迁移,DbMigration lastMigration)在 System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable 1 pendingMigrations, 字符串 targetMigrationId, 字符串 lastMigrationId)
在 System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable 1 pendingMigrations, 字符串 targetMigrationId, 字符串 lastMigrationId)
在 System.Data.Entity.Migrations.DbMigrator.UpdateInternal(字符串 目标迁移)在 System.Data.Entity.Migrations.DbMigrator.c__DisplayClasse.b__d() 在 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作 mustSucceedToKeepDatabase) 在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(操作 mustSucceedToKeepDatabase) 在 System.Data.Entity.Migrations.DbMigrator.Update(字符串 目标迁移)在 System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(字符串 目标迁移)在 System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 在 System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 输入字符串的格式不正确。

如何重复:
使用瑞典语 Windows(或任何其他不使用“.”作为小数分隔符的语言)。

创建带有索引的迁移文件,如:

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

运行数据库更新

(错误报告已发送至 MySql:https://bugs.mysql.com/bug.php?id=92561

【问题讨论】:

    标签: mysql .net entity-framework


    【解决方案1】:

    此错误是由于 MySqlMigrationSqlGenerator.Generate(CreateIndexOperation op) 通过将字符串转换为双精度来检查数据库的版本。但是,它在没有指定 IFormatProvider 的情况下这样做。由于瑞典语使用“,”作为小数分隔符,因此转换失败(版本号由“.”分隔)。

    通过覆盖 MySqlMigrationSqlGenerator.Generate(CreateIndexOperation op) 可以避免这种情况。使用此答案https://stackoverflow.com/a/51756143/1037864 中的代码(这是另一个问题的答案)

    【讨论】:

    • 你说得对,但更好的答案应该是 BlackGad 在你引用的答案中的评论 stackoverflow.com/a/54707831/5250701
    • 感谢@NicolasC,这似乎是一个很好的解决方案。由于我自己没有尝试过,所以我现在不会改变我的答案。此外,根据我的错误票证的答案,这现在应该在版本 8.0.19 中开箱即用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 2018-03-02
    • 1970-01-01
    • 2012-03-03
    • 2014-09-30
    相关资源
    最近更新 更多