-
Notifications
You must be signed in to change notification settings - Fork 243
Milestone
Description
Problem:
The following exception is thrown when selecting DateInterval.End:
InvalidCastException: Reading as 'NodaTime.LocalDate' is not supported for fields having DataTypeName 'timestamp without time zone'
Entity
public class User
{
public DateInterval IntervalField { get; set; } = new(LocalDate.MinIsoValue, LocalDate.MaxIsoValue);
}
Query
var result = await dbContext.Set<User>()
.Select(x => x.IntervalField.End)
.FirstOrDefaultAsync();
}
Cause:
Accessing DateInterval.End generates the following SQL:
... upper(u.interval_field) - INTERVAL 'P1D' ...
The result is of type 'timestamp without time zone'. This should be casted to date.
Workaround:
You can force EF to generate the correct sql:
Working query
var result = await dbContext.Set<User>()
.Select(x => (LocalDate)(object)x.IntervalField.End)
.FirstOrDefaultAsync();
}
This generates the following SQL:
... CAST(upper(u.interval_field) - INTERVAL 'P1D' AS date) ...
Metadata
Metadata
Assignees
Labels
No labels