Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when selecting NodaTime DateInterval.End #3015

Closed
haas-daniel opened this issue Dec 12, 2023 · 0 comments · Fixed by #3024
Closed

Exception when selecting NodaTime DateInterval.End #3015

haas-daniel opened this issue Dec 12, 2023 · 0 comments · Fixed by #3024
Assignees
Labels
bug Something isn't working
Milestone

Comments

@haas-daniel
Copy link
Contributor

haas-daniel commented Dec 12, 2023

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) ...
haas-daniel added a commit to phoenis-novellus/efcore.pg that referenced this issue Dec 12, 2023
roji pushed a commit to roji/efcore.pg that referenced this issue Dec 16, 2023
@roji roji added the bug Something isn't working label Dec 16, 2023
@roji roji added this to the 8.0.1 milestone Dec 16, 2023
roji added a commit that referenced this issue Dec 16, 2023
Fixes #3015

Co-authored-by: Daniel Haas <dhaas@phoenis.de>
roji added a commit that referenced this issue Dec 19, 2023
Fixes #3015

Co-authored-by: Daniel Haas <dhaas@phoenis.de>
(cherry picked from commit f9e3428)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants