I have successfully configured a DateOnly
property on my EF 6 entity as below
Everything is working great, except for the fact that the property is nullable. The issue occurs when I try to filter entities where Prperty1 < [some-date]
, in that EF does not consider null
to be less than anything, so it is excluded from the results
How can I get EF to understand that null
is basically less than or equivalent to DateOnly.MinValue
when filtering on that property?
I already tried replacing DateOnly
with DateOnly?
in my converter, but it makes no difference
public class MyEntity
{
public DateOnly? Property1 { get; private set; }
}
Configuration:
builder
.Property(x => x.Property1)
.HasConversion(DateOnlyRelationalTypeMapping.Converter)
.HasColumnType("date");
Relational type mapping:
public sealed class DateOnlyRelationalTypeMapping : RelationalTypeMapping
{
internal DateOnlyRelationalTypeMapping()
: this(
new RelationalTypeMappingParameters(
new CoreTypeMappingParameters(
typeof(DateOnly),
Converter),
"date"))
{
}
private DateOnlyRelationalTypeMapping(RelationalTypeMappingParameters parameters)
: base(parameters)
{
}
public static new readonly ValueConverter<DateOnly, DateTime> Converter =
new(
clrValue => clrValue.ToDateTime(TimeOnly.MinValue),
dbValue => DateOnly.FromDateTime(dbValue));
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new DateOnlyRelationalTypeMapping(parameters);
}