Skip to content

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

Closed

Description

@pil0t

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

Activity

  1. roji commented on Sep 26, 2023

    @roji
    Member

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

  2. akmalnodirov commented on Nov 19, 2023

    @akmalnodirov
  3. self-assigned this
    on Nov 19, 2023
  4. added a commit that references this issue on Nov 19, 2023
    d7cdbac
  5. added this to the 8.0.0 milestone on Nov 19, 2023
  6. 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[/+] on Nov 19, 2023
  7. added a commit that references this issue on Nov 19, 2023
    4464d47
  8. roji commented on Nov 19, 2023

    @roji
  9. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions