达梦数据库,从实体类映射到到匿名类时,数据库 TIME 类型无法转换为 TimeSpan 返回

SqlSugar 沟通中
2 762
using SqlSugar;

using SqlSugarClient client = new(new ConnectionConfig
{
    DbType = DbType.Dm,
    ConnectionString = "***"
});
var result = await client
    .Queryable<TableEntity>()
    .Select(x => new
    {
        x.TimeSpanProperty
    })
    .FirstAsync();
Console.WriteLine(result?.TimeSpanProperty);

[SugarTable("***")]
public class TableEntity
{
    [SugarColumn(ColumnName = "***")]
    public TimeSpan? TimeSpanProperty { get; set; }
}

以上代码引发异常的错误堆栈为

Newtonsoft.Json.JsonSerializationException: Error converting value 01/04/2026 14:00:00 to type 'System.Nullable`1[System.TimeSpan]'. Path 'ArrivalTime', line 1, position 173.
 ---> System.ArgumentException: Could not cast or convert from System.DateTime to System.TimeSpan.
   at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
   at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   --- End of inner exception stack trace ---
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor`1 creator, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at SqlSugar.SerializeService.DeserializeObject[T](String value)
   at SqlSugar.ContextMethods.DeserializeObject[T](String value)
   at SqlSugar.ContextMethods.DataReaderToListAsync[T](IDataReader reader)
   at SqlSugar.QueryableProvider`1.GetDataAsync[TResult](Boolean isComplexModel, Type entityType, IDataReader dataReader)
   at SqlSugar.QueryableProvider`1.GetDataAsync[TResult](KeyValuePair`2 sqlObj)
   at SqlSugar.QueryableProvider`1._ToListAsync[TResult]()
   at SqlSugar.QueryableProvider`1.ToArrayAsync()

修改了源码进行测试,在此处

https://github.com/DotNetNext/SqlSugar/blob/65b07fe57ceb968e85cf0665dfe7ece9c09e8402/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs#L605-L609

在 .NET 10 的测试项目中,附加以下额外处理,可以解决此问题

else if (type.FullName == "System.TimeOnly" || type.FullName == "System.TimeSpan")
{
    addValue = Convert.ToDateTime(addValue).ToString("HH:mm:ss");
}



热忱回答(2)

  • SqlSugarCore 5.1.4.212-preview02


    过五分钟后安装212预览版本已修复

    0 回复
  • @fate sta:

    image.png

    https://github.com/DotNetNext/SqlSugar/commit/4bb2786072b88aae4b117f0e3cdcd0ef3d2d222c

    检查您的提交发现,似乎是直接将对于 System.TimeSpan 的处理合并进了 System.DateOnly 的处理分支之内,这导致了对于 TimeSpan 类型,也使用仅日期的格式进行反序列化。

    对于 System.TimeOnly 和 System.TimeSpan 的处理应该是与 System.DateOnly 不同的,修改为以下代码测试可以取值正确。

    image.png

    辛苦您再修正一下,感谢 

    0 回复