postgresql 带Postgres的.Net Core 7无法将带时区的数据库类型时间戳强制转换为LocalDateTime

kzipqqlq  于 2023-03-08  发布在  PostgreSQL
关注(0)|答案(1)|浏览(136)

我读过一些非常类似的帖子,但我还没有找到答案。
我尝试做的是从数据库中获取时间戳,将其转换为用户的时区,然后将其转换为字符串-最好是在EF Linq语句中。
下面是一个对我来说失败的较小Linq语句的示例。

model.PreviousClasses = await _context.ClassOfferingEnrollments.AsNoTracking().Where(x =>
                    x.WorkerId == workerId
                    && x.Offering != null
                    && !x.ArchivedDate.HasValue
                    && (x.Offering.ElectionId == null || x.Offering.Election.ElectionDate < currentDate))
                .Select(x => new PreviousClassesViewModel()
                {
                    ElectionName = x.Offering.Election.Name,
                    LocationName = x.Offering.Location.Name,
                    ClassOfferingType = x.Offering.Type,
                    StartTime = x.Offering.ClassStartTime.Value.InZone(_currentCountyTimeZone).LocalDateTime,
                    EndTime = x.Offering.ClassEndTime.Value.InZone(_currentCountyTimeZone).LocalDateTime,
                    InstructorName = x.Offering.Instructor != null
                        ? x.Offering.Instructor.FirstName + "_" + x.Offering.Instructor.LastName
                        : null
                }).ToListAsync();

失败的部分是InZone(_currentCountyTimeZone)行。我也尝试过这个:

x.Offering.ClassStartTime.Value.InZone(DateTimeZoneProviders.Tzdb["America/New_York"]).LocalDateTime

我还读到InZone有一些问题,所以我也尝试从另一个帖子:

x.Offering.ClassStartTime.Value.InUtc().LocalDateTime

所有这些查询都给予相同的异常:

{"Can't cast database type timestamp with time zone to LocalDateTime"}

我读过其他几篇文章,所以我也设置了一些其他的东西。我的创业公司有这些东西:

AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

services.AddEntityFrameworkNpgsql()
                .AddDbContext<ElectionWorkerPortalContext>(options => options.UseNpgsql(defaultConnectionString, o => o.UseNodaTime()))

我还尝试通过以下方式在我的构建器上设置此设置:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseNpgsql(o => o.UseNodaTime());

但是UseNodaTime()并不作为该对象上的方法存在。
我已经通过Nuget安装了以下内容:

<ItemGroup>
    <PackageReference Include="AWSSDK.S3" Version="3.7.103.1" />
    <PackageReference Include="AWSSDK.SecretsManager" Version="3.7.101.21" />
    <PackageReference Include="CsvHelper" Version="12.1.2" />
    <PackageReference Include="DataTables.AspNet.AspNetCore" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="7.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
    <PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.3.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.3" />
    <PackageReference Include="Mindscape.Raygun4Net.AspNetCore" Version="6.6.6" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
    <PackageReference Include="NodaTime.Serialization.JsonNet" Version="3.0.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime" Version="7.0.3" />
    <PackageReference Include="Rotativa.AspNetCore" Version="1.2.0" />
    <PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
    <PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
    <PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
    <PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
    <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
    <PackageReference Include="System.Drawing.Common" Version="7.0.0" />
  </ItemGroup>

我想这些就是所有的细节。如果有必要的话,我不介意安装另一个软件包,唯一不愿意做的就是降级. Net。我应该指出,在这次升级之前,我使用的是core3.1,一切都正常。我是一个有经验的开发人员,但远不是时区Maven(它的复杂),所以任何援助是受欢迎的!我相信我有最新的一切目前,我只是想让它再次稳定。谢谢!

vhmi4jdf

vhmi4jdf1#

最后我创建了一个扩展方法,它似乎完成了这项工作。而不是调用:

.InZone(_currentTimeZone)

其中_currentTimeZone是一个DateTimeZone。我现在这样调用我的扩展:

.InZoneExtension(_currentTimeZone)

只是现在_currentTimeZone是一个字符串,我是这样使用它的:

public static ZonedDateTime InZoneExtension(this Instant instant, string timezone)
        {
            DateTimeZone zone = DateTimeZoneProviders.Tzdb[timezone];

            return instant.InZone(zone);
        }

希望这能帮助到一些人。如果有人知道更好的解决方案,我仍然很想知道。

相关问题