public class CustomMapper : ConventionMapper
{
private static readonly Type DateOnlyType = typeof(DateOnly);
private static readonly Type DateTimeType = typeof(DateTime);
public override Func<object, object> GetFromDbConverter(PropertyInfo targetProperty, Type sourceType)
{
// see if there's already a converter on the class/property
var result = base.GetFromDbConverter(targetProperty, sourceType);
if (result == null
&& targetProperty.PropertyType == DateOnlyType
&& sourceType == DateTimeType)
{
return o => DateOnly.FromDateTime((DateTime)o);
}
return result;
}
public override Func<object, object> GetToDbConverter(PropertyInfo sourceProperty)
{
// see if there's already a converter on the class/property
var result = base.GetToDbConverter(sourceProperty);
if (result == null
&& sourceProperty.PropertyType == DateOnlyType)
{
return o => ((DateOnly)o).ToDateTime(TimeOnly.MinValue);
}
return result;
}
}
var dbConfig = DatabaseConfiguration
.Build()
.UsingConnectionString(Environment.GetEnvironmentVariable("DbConnection"))
.UsingDefaultMapper(new CustomMapper())
.UsingProvider<SqlServerDatabaseProvider>()
;