带有 MySQL 的 EF6 - 日期时间中的格式异常

EF6 with MySQL - formatexception in datetime

我有这样的数据库编码:

public class DBModel : DbContext
    {
        public DBModel()
            : base("name=DBModel")
        {
        }

        public virtual DbSet<Entry> Entries{ get; set; }
    }

    public class Entry
    {
        [Key]
        public int Id { get; set; }
        public DateTime EntryDate { get; set; }
        public string EntryContent { get; set; }
        public virtual Alarm Alarm { get; set; }
    }

    public class Alarm
    {
        [Key]
        public int AlarmId { get; set; }
        public DateTime AlarmDate { get; set; }
        public bool Enabled { get; set; }
    }

当我尝试更新数据库时,它以 FormatException 结束,堆栈如下:

System.FormatException: Nieprawidłowy format ciągu wejściowego.
   w System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
   w System.Convert.ToDouble(String value)
   w MySql.Data.Entity.MySqlMigrationSqlGenerator.Generate(CreateIndexOperation op)
   w MySql.Data.Entity.MySqlMigrationSqlGenerator.Generate(IEnumerable`1 migrationOperations, String providerManifestToken)
   w System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
   w System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, VersionedModel sourceModel, VersionedModel targetModel, Boolean downgrading)
   w System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String migrationId, VersionedModel sourceModel, VersionedModel targetModel, Boolean downgrading)
   w System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   w System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   w System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   w System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   w System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   w System.Data.Entity.Infrastructure.Design.Executor.Update.<>c__DisplayClass0_0.<.ctor>b__0()
   w System.Data.Entity.Infrastructure.Design.Executor.OperationBase.Execute(Action action)

EF框架版本:6.4.4 MySQL服务器版本:8.0.18 MySQL 插件:

如何解决?

这看起来像是 MySql.Data.EntityFramework 中的错误,由以下行引起:https://github.com/mysql/mysql-connector-net/blob/f3b6ae6a416de898b25edc0062b25a2f6ea90291/EntityFramework/src/MySqlMigrationSqlGenerator.cs#L771

我猜 _providerManifestToken 包含类似 "8.0" 的字符串,但您的程序 运行ning 在浮点数所在的语言环境 (pl-PL?) 中格式如 8,0。因此,对 Convert.ToDouble 的文化敏感调用失败。

您可以在 https://bugs.mysql.com 将此作为错误报告并等待修复。

或者,可以将 Thread.CurrentCulture 设置为 en-US 到 运行 迁移 (Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");),然后再设置回来。