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!;
}
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)(wheree.UtcTimestampisDateTimeOffset). This interceptor worked fine withNpgsql.EntityFrameworkCore.PostgreSQLversions10.0.*and produced nicee."Timestamp" AT TIME ZONE e."TimeZoneId", but on11.0.0-rc.1it fails with exceptions likeI think this might be due to accessing
UtcDateTimeno longer causing EF to recognize the change of type fromDateTimeOffsettoDateTime? 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: