Mapping Postgres time type to c# DateTime with Dapper

Viewed 22

I have a Postgres db with a "time" field. Data is stored like "16:00:00".

C# does not have a Time type so I use DateTime.

However, I am getting an error when the time field has a value:

Error parsing column 71 (my_time_field=16:00:00 - Object) ---> System.InvalidCastException: Specified cast is not valid.

So Dapper is not able to automatically map a nullable Time field to a nullable C# DateTime property.

Is there a way to tell Dapper how to handle this one particular field? Currently I handle this when using a DataReader by just prepending a date like this:

string time = reader["my_time_field"].ToString();
var dateTime = DateTime.Parse($"01/01/0001 {time}");
0 Answers
Related