Skip to content

Exception when selecting NodaTime DateInterval.End #3015

@haas-daniel

Description

@haas-daniel

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
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions