简体   繁体   English

无法将没有时区的数据库类型时间戳转换为 Instant

[英]Can't cast database type timestamp without time zone to Instant

reservationLogs = await this.dbContext.ReservationLogs
                   .Where(r => r.ProcessedAt == null)
                   .Where(r => r.PropertyId == validPropertyId)
                   .OrderBy(r => r.CreatedAt).ThenBy(r => r.Operation)
                   .Take(200)
                   .ToListAsync();

some times with the same query i get the error有时使用相同的查询我得到错误

' Can't cast database type timestamp without time zone to Instant' '不能将没有时区的数据库类型时间戳转换为 Instant'

note: CreatedAt nodaTime instane注意:CreatedAt nodaTime 实例

i am trying to find the exact reason我正在寻找确切的原因

You can do cast using Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime package您可以使用Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime package 进行投射

protected override void OnConfiguring(DbContextOptionsBuilder builder) { builder.UseNpgsql("connection-string", o => o.UseNodaTime()); }

or:或者:

builder.Services.AddDbContext<ApplicationDbContext>(
    options => options.UseNpgsql(
        builder.Configuration.GetConnectionString("DefaultConection"),
        o => o.UseNodaTime()));

source资源

The issue is that even though the date and time is clear, it is unclear whether or which timezone was in use.问题是,即使日期和时间很清楚,也不清楚是否使用了或使用了哪个时区。 If I tell you that tomorrow at 5 PM I will go for a walk, then it will be unclear from your perspective what the exact time it will be, unless you know what timezone was I assuming while saying so.如果我告诉你明天下午 5 点我将 go 散步,那么从你的角度来看,它会是什么确切时间将是不清楚的,除非你知道我在这么说时假设的时区。

You have the exact same type of confusion in your code and first, you need to install this plugin: https://www.npgsql.org/doc/types/nodatime.html您的代码中有完全相同的混淆类型,首先,您需要安装此插件: https://www.npgsql.org/doc/types/nodatime.html

According to the docs, you need to add a dependency like this:根据文档,您需要添加这样的依赖项:

using Npgsql;

// Place this at the beginning of your program to use NodaTime everywhere (recommended)
NpgsqlConnection.GlobalTypeMapper.UseNodaTime();

// Or to temporarily use NodaTime on a single connection only:
conn.TypeMapper.UseNodaTime();

The docs go further in specifying how you can read and write values:文档 go 进一步指定了如何读取和写入值:

// Write NodaTime Instant to PostgreSQL "timestamp with time zone" (UTC)
using (var cmd = new NpgsqlCommand(@"INSERT INTO mytable (my_timestamptz) VALUES (@p)", conn))
{
    cmd.Parameters.Add(new NpgsqlParameter("p", Instant.FromUtc(2011, 1, 1, 10, 30)));
    cmd.ExecuteNonQuery();
}

// Read timestamp back from the database as an Instant
using (var cmd = new NpgsqlCommand(@"SELECT my_timestamptz FROM mytable", conn))
using (var reader = cmd.ExecuteReader())
{
    reader.Read();
    var instant = reader.GetFieldValue<Instant>(0);
}

Since you are not directly writing the query, but use the Entity Framework to do so you have another level of expression.由于您不是直接编写查询,而是使用实体框架来执行此操作,因此您有另一个级别的表达式。 But this is well-documented as well.但这也有据可查。 You can safely and soundly declare types like this:您可以像这样安全可靠地声明类型:

public LocalDate Date {get; set;}

Read this full article: https://www.davepaquette.com/archive/2019/03/26/using-noda-time-with-ef-core.aspx阅读全文: https://www.davepaquette.com/archive/2019/03/26/using-noda-time-with-ef-core.aspx

You will need to find out exactly where the error occurs.您将需要准确找出错误发生的位置。 It seems to me that the OrderBy is the culprit as well as the selection.在我看来, OrderBy是罪魁祸首,也是选择。 You can change the type of the data member of your model.您可以更改 model 的数据成员的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PostgreSQL中存储时间戳会导致错误:“无法将类型bigint转换为没有时区的时间戳” - Storing timestamp in PostgreSQL gives an error: “cannot cast type bigint to timestamp without time zone” PHP / PDO无法绑定“ TIMESTAMP WITHTIME TIME ZONE”值 - PHP/PDO can't bind 'TIMESTAMP WITHOUT TIME ZONE' values ActiveRecord::StatementInvalid: PG::DatatypeMismatch: 错误: 列“completed_at”不能自动转换为没有时区的类型时间戳 - ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "completed_at" cannot be cast automatically to type timestamp without time zone Rails&Postgres:迁移到change_colomn会出现错误“无法转换为没有时区的类型时间戳” - Rails & Postgres: Migration to change_colomn gives error “cannot be cast to type timestamp without time zone” PostgreSQL改变没有时区的类型时间戳 - >带时区 - PostgreSQL alter type timestamp without time zone -> with time zone PostgresSQL + Spring JPA:org.postgresql.util.PSQLException:错误:无法将类型 bytea 转换为没有时区的时间戳 - PostgresSQL + Spring JPA: org.postgresql.util.PSQLException: ERROR: cannot cast type bytea to timestamp without time zone Django 错误:无法将类型日期转换为没有时区的时间 - Django error: cannot cast type date to time without time zone ProgrammingError:无法将双精度类型强制转换为没有时区的时间 - ProgrammingError: cannot cast type double precision to time without time zone 列的时间戳类型没有时区,但表达式的类型为字符 - Column is of type timestamp without time zone but expression is of type character 列“生日”是没有时区的时间戳类型,但表达式是文本类型 - column "birthdate" is of type timestamp without time zone but expression is of type text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM