MySql 异常 使用 DateTimeOffset.UtcNow

问题描述

我在 .net framework 4.8 和 Oracle 的 mysql-connector-net 提供程序上使用 EF6(MysqL.Data 和 MysqL.Data.EntityFramework nuget 包的 v8.0.23)

考虑这个代码

DateTimeOffset threshold = DateTimeOffset.UtcNow;
List<Domain.Entities.Security.openiddicttoken> query = await _dbContext.openiddicttokens.Where(token => token.CreationDate < threshold.UtcDateTime).ToListAsync();

使用这个类

public partial class openiddicttoken {
    ///...other properties
    public DateTime? CreationDate { get; set; }
}

数据库中的字段是:

  `ExpirationDate` datetime(6) DEFAULT NULL,

产生这个异常(顶部,我省略了异步部分信息):

System.Data.Entity.Core.EntityCommandExecutionException:错误 执行命令定义时发生。看内部 详情除外。 ---> MysqL.Data.MysqLClient.MysqLException: 命令执行期间遇到致命错误。 ---> MysqL.Data.MysqLClient.MysqLException:无法序列化日期/时间 价值。 在 MysqL.Data.Types.MysqLDateTime.MysqL.Data.Types.IMysqLValue.WriteValue(MysqLPacket 数据包,布尔二进制,对象值,Int32 长度) 在 MysqL.Data.MysqLClient.MysqLParameter.Serialize(MysqLPacket 数据包,布尔二进制,MysqLConnectionStringBuilder 设置) 在 MysqL.Data.MysqLClient.Statement.SerializeParameter(MysqLParameterCollection 参数、MysqLPacket 数据包、String parmName、Int32 parameterIndex) 在 MysqL.Data.MysqLClient.Statement.InternalBindParameters(String sql,MysqLParameterCollection 参数,MysqLPacket 包) 在 MysqL.Data.MysqLClient.Statement.BindParameters() 在 MysqL.Data.MysqLClient.PreparableStatement.Execute() 在 MysqL.Data.MysqLClient.MysqLCommand.ExecuteReader(CommandBehavior 行为)

似乎只有在我使用 DateTimeOffset 的 UtcDateTime 时才会抛出异常。如果我使用阈值.DateTime,它似乎工作正常。 奇怪的是,这两个属性都返回一个 DateTime 类型。

我的问题是此代码是包 (openiddict) 的一部分,因此我无法更改 C# 代码。我只能更改 nuget 包版本和数据库布局。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)