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

Version 6.0.4+ causes translation failure of DateTime.Date when mapped to PG date #2499

Closed
SeanLeitzinger opened this issue Sep 7, 2022 · 7 comments · Fixed by #2500
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@SeanLeitzinger
Copy link

SeanLeitzinger commented Sep 7, 2022

Using 6.0.3 works as expected. When upgrading to 6.0.4 or higher, all of my LINQ queries that use DateTime as a predicate break. The column type is "date". An example of the error:

System.InvalidOperationException: The LINQ expression 'DbSet()
.Where(p => !(p.IsDeleted))
.Where(p => p.TransferDate != null && p.TransferDate.Value.Date.Year == __year_0)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.

The C# code:
var payments = await context.Payments.Where(x => x.TransferDate != null && x.TransferDate.Value.Date.Year == year).ToListAsync();

I'm having trouble finding the change in 6.0.4 that precipitated this issue. Is this a known breaking change?

@roji
Copy link
Member

roji commented Sep 7, 2022

I'm not able to reproduce the problem, please see the code below. Can you please submit a minimal, runnable code sample which reproduces this, or tweak the below to make it fail?

Attempted repro
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

var year = 2022;
var payments = await context.Payments.Where(x => x.TransferDate != null && x.TransferDate.Value.Date.Year == year).ToListAsync();

public class BlogContext : DbContext
{
    public DbSet<Payment> Payments { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseNpgsql(@"Host=localhost;Username=test;Password=test")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Payment>().HasQueryFilter(p => !p.IsDeleted);
    }
}

public class Payment
{
    public int Id { get; set; }
    public bool IsDeleted { get; set; }
    public DateTime? TransferDate { get; set; }
}

@SeanLeitzinger
Copy link
Author

Add the following to OnModelCreating:
modelBuilder.Entity().Property(x => x.TransferDate)
.HasColumnType("date");

This reproduces the issue. Thanks.

@roji
Copy link
Member

roji commented Sep 7, 2022

Thanks, can see the failure now - will investigate.

@roji
Copy link
Member

roji commented Sep 7, 2022

Out of curiosity, any particular reason you're calling Date there, given that TransferDate already represents a date? That's guaranteed to do nothing, and removing it makes the query work.

(I'll still fix this though)

@roji roji changed the title Version 6.0.4+ Causes Date Queries To Fail Version 6.0.4+ causes translation failure of DateTime.Date when mapped to PG date Sep 7, 2022
@roji roji self-assigned this Sep 7, 2022
@roji roji added the bug Something isn't working label Sep 7, 2022
@roji roji added this to the 6.0.7 milestone Sep 7, 2022
@roji
Copy link
Member

roji commented Sep 7, 2022

This happened as a result of #2336, which make the translation of DateTime.Date vary based on the type mapping (timestamp vs. timestamptz); date was left out.

@SeanLeitzinger
Copy link
Author

SeanLeitzinger commented Sep 7, 2022

Out of curiosity, any particular reason you're calling Date there, given that TransferDate already represents a date? That's guaranteed to do nothing, and removing it makes the query work.

(I'll still fix this though)

I'm not sure why I did it on that query. I don't do it anywhere else in the system now that I look at it. Possibly a mistake, or habit from doing it in other systems that are SQL Server based. Thanks for pointing it out since I can finally update now to 6.0.6.

@roji
Copy link
Member

roji commented Sep 7, 2022

Sure thing. In any case, I'm preparing a fix that would simply ignore that Date property access, when the DateTime is mapped to a PG date.

roji added a commit to roji/efcore.pg that referenced this issue Sep 7, 2022
roji added a commit to roji/efcore.pg that referenced this issue Sep 7, 2022
@roji roji closed this as completed in #2500 Sep 7, 2022
roji added a commit that referenced this issue Sep 7, 2022
roji added a commit that referenced this issue Sep 7, 2022
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.

2 participants