This does not work:
public class DateTimeHandler : SqlMapper.TypeHandler<DateTime>
{
public override DateTime Parse(object value) =>
DateTime.SpecifyKind((DateTime)value, DateTimeKind.Utc);
public override void SetValue(IDbDataParameter parameter, DateTime value)
{
parameter.Value = value;
}
}
Dapper.SqlMapper.AddTypeHandler<DateTime>(new DateTimeHandler());
var res = connection.QuerySingleOrDefault<dynamic>($@"SELECT some_date FROM table");
// outputs Unspecified
System.Console.WriteLine(res.some_date.Kind.ToString());
This works:
public class ViewModel {
public DateTime some_date { get; set; }
}
var res = connection.QuerySingleOrDefault<ViewModel>($@"SELECT some_date FROM table");
// outputs Utc
System.Console.WriteLine(res.some_date.Kind.ToString());
Any way to make typehandlers in dapper work when querying as dynamic?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…