I am trying to add "UTC" conversion to all my Inserted/Retrieved dates with Dapper. Right now all dates are inserted as local dates. After some research I found a way to do it with retrieved values, by implementing TypeHandler as follows:
public class DateTimeHandler : SqlMapper.TypeHandler<DateTime>
{
public override void SetValue(IDbDataParameter parameter, DateTime value)
{
parameter.Value = DateTime.SpecifyKind(value, DateTimeKind.Utc);
}
public override DateTime Parse(object value)
{
return DateTime.SpecifyKind((DateTime)value, DateTimeKind.Utc);
}
}
The problem is with Insertion, it looks like SetValue is never called. What's the purpose of the method SetValue ? How to convert all DateTime values passed to SQL With dapper?