Skip to content

Translation of TimeZoneInfo.ConvertTimeBySystemTimeZoneId fails when used together with DateTimeOffset.UtcDateTime #3912

Description

@Saancreed

Hello,

I have an interceptor which translates access to some helper properties on my value object to a chain of methods/property accesses that looks more or less like e => TimeZoneInfo.ConvertTimeBySystemTimeZoneId(e.UtcTimestamp.UtcDateTime, e.TimeZoneId) (where e.UtcTimestamp is DateTimeOffset). This interceptor worked fine with Npgsql.EntityFrameworkCore.PostgreSQL versions 10.0.* and produced nice e."Timestamp" AT TIME ZONE e."TimeZoneId", but on 11.0.0-rc.1 it fails with exceptions like

System.InvalidOperationException: The LINQ expression 'DbSet<Entity>()
    .OrderBy(e => TimeZoneInfo.ConvertTimeBySystemTimeZoneId(
        dateTime: e.UtcTimestamp.UtcDateTime, 
        destinationTimeZoneId: e.TimeZoneId))' could not be translated. Additional information: Translation of method 'System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. 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'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

I think this might be due to accessing UtcDateTime no longer causing EF to recognize the change of type from DateTimeOffset to DateTime? Because inserting additional (and redundant) .ToUniversalTime() works around this. I may be running into some "don't use datetimes like this" territory, but so far this worked for me okay-ish, so I'm a little surprised that it doesn't anymore.

Minimal repro:

#!/usr/bin/env -S dotnet run --file
#:property TargetFramework=net11.0
#:property PublishAot=false
#:property PublishTrimmed=false
#:property RestoreAdditionalProjectSources=https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet11/nuget/v3/index.json
#:package Npgsql.EntityFrameworkCore.PostgreSQL@11.0.0-rc.1
// Works with:
// #:package Npgsql.EntityFrameworkCore.PostgreSQL@10.0.3

using Microsoft.EntityFrameworkCore;

using var db = new ReproContext();

var query = db.Entities.OrderBy(e =>
    TimeZoneInfo.ConvertTimeBySystemTimeZoneId(e.UtcTimestamp.UtcDateTime, e.TimeZoneId)
);

Console.WriteLine(query.ToQueryString());

sealed class ReproContext : DbContext
{
    public DbSet<Entity> Entities => Set<Entity>();

    protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseNpgsql();
}

sealed class Entity
{
    public int Id { get; set; }
    public DateTimeOffset UtcTimestamp { get; set; }
    public string TimeZoneId { get; set; } = null!;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions