【问题标题】:EF6 with MySQL - formatexception in datetimeEF6 与 MySQL - 日期时间中的格式异常
【发布时间】:2020-11-08 21:33:10
【问题描述】:

我有这样的 DB 编码:

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 v6.10.9
  • MySQL.Data v6.10.9

如何解决?

【问题讨论】:

  • 在破坏更新数据库之前是否添加了迁移?

标签: c# mysql entity-framework ef-code-first


【解决方案1】:

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

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

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

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

【讨论】:

  • 是的,在波兰,我们使用逗号作为小数分隔符 ;) 我会尝试更改迁移设置 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
相关资源
最近更新 更多