[英]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()));
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.