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’
note: CreatedAt nodaTime instane
i am trying to find the exact reason
2
Answers
You can do cast using Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime package
or:
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 P.M. 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.
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
According to the docs, you need to add a dependency like this:
The docs go further in specifying how you can read and write values:
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:
Read this full article: 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. You can change the type of the data member of your model.