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

DateOnly.AddDays, Months and Years can't be projected out #2888

Closed
pil0t opened this issue Sep 26, 2023 · 3 comments · Fixed by #2964
Closed

DateOnly.AddDays, Months and Years can't be projected out #2888

pil0t opened this issue Sep 26, 2023 · 3 comments · Fixed by #2964
Assignees
Labels
bug Something isn't working
Milestone

Comments

@pil0t
Copy link

pil0t commented Sep 26, 2023

Here is the code to reproduce the bug:

using Microsoft.EntityFrameworkCore;
using Npgsql;
using Xunit;

namespace DateOnlyNpgsql;

public class TestEntity
{
    public int Id { get; set; }
    public DateOnly Date { get; set; }
    public int Diff { get; set; }
}

public class MyTest
{
    [Fact]
    public async Task InvalidCastExample()
    {
        var x = new DbCtx();
        await x.Database.EnsureDeletedAsync();
        await x.Database.EnsureCreatedAsync();
        var e = new TestEntity()
        {
            Id = 1,
            Date = DateOnly.Parse("2023-09-26"),
            Diff = 1
        };
        x.TestEntities.Add(e);
        await x.SaveChangesAsync();

        var query = x.TestEntities
            .Select(x => new
            {
                x.Id,
                NextMonth = x.Date.AddDays(x.Diff)
            });

        // System.InvalidCastException: Can't cast database type timestamp without time zone to DateOnly
        var a1 = query.ToArray();
    }

}

public class DbCtx : DbContext
{
    public DbSet<TestEntity> TestEntities { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseNpgsql(
            new NpgsqlConnection("Host=localhost;Uid=postgres;Pwd=mysecretpassword;Database=DummyDatabase"));
}

Tested on 6.0.22 and 7.0.11

@roji
Copy link
Member

roji commented Sep 26, 2023

Thanks @pil0t, I can see this bug and will work on a fix.

@akmalnodirov

This comment was marked as off-topic.

@roji roji self-assigned this Nov 19, 2023
roji added a commit to roji/efcore.pg that referenced this issue Nov 19, 2023
@roji roji added the bug Something isn't working label Nov 19, 2023
@roji roji added this to the 8.0.0 milestone Nov 19, 2023
@roji roji changed the title InvalidCastException: Can't cast database type timestamp without time zone to DateOnly DateOnly.AddDays, Months and Years can't be projected out Nov 19, 2023
roji added a commit that referenced this issue Nov 19, 2023
@roji

This comment was marked as off-topic.

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
Development

Successfully merging a pull request may close this issue.

3 participants