某些情况下映射到 TimeSpan/TimeOnly 失败 返回

SqlSugar 待处理
90
const string connectionString = "Server=x.x.x.x:xxxx;User Id=xxxxxx;PWD=xxxx;SCHEMA=xxxx;DATABASE=xxxx";
using SqlSugarClient client = new(new ConnectionConfig
{
    DbType = DbType.Dm,
    ConnectionString = connectionString
});
var result = await client.Queryable<SomeEntity>()
    .Select(x => new
    {
        Entity = x
    })
    .Take(1)
    .ToListAsync();

[SugarTable("xxxx")]
public class SomeEntity
{
    [SugarColumn(ColumnName = "xxxx")]
    public TimeSpan? SomeTimeSpanField { get; set; }
}


image

image

Newtonsoft.Json.JsonSerializationException: Error converting value 2026-08-02 18:00:00 to type 'System.Nullable`1[System.TimeSpan]'. Path 'Entity.SomeTimeSpanField', line 1, position 52.
 ---> 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.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, 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.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.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
   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.JsonSerializer.Deserialize(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) in D:\Codes\GitHub\SqlSugar\Src\Asp.NetCore2\SqlSugar\IntegrationServices\SerializeService.cs:line 56
   at SqlSugar.ContextMethods.DeserializeObject[T](String value) in D:\Codes\GitHub\SqlSugar\Src\Asp.NetCore2\SqlSugar\Infrastructure\ContextMethods.cs:line 962
   at SqlSugar.ContextMethods.<DataReaderToListAsync>d__21`1.MoveNext() in D:\Codes\GitHub\SqlSugar\Src\Asp.NetCore2\SqlSugar\Infrastructure\ContextMethods.cs:line 392



参见 https://github.com/DotNetNext/SqlSugar/pull/1451

热忱回答0