question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

10.0 PostgreSql InvalidCastException with DateTime

See original GitHub issue

I encountered the following error when trying to launch a brand new project with v10.0 and PostgreSql :

InvalidCastException : Cannot write DateTime with Kind=Local to PostgreSQL type ‘timestamp with time zone’, only UTC is supported.

To resolve it I needed to add the following line of code in the ConfigureServices method : System.AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

I don’t know if it’s a good way to resolve this but at least it work’s for now.

Resources I’ve found about it : https://github.com/npgsql/efcore.pg/issues/2000 https://www.npgsql.org/efcore/release-notes/6.0.html

Hope it helps.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gyfkecommented, Nov 7, 2022

This is unfortunate as the solution was taken from the Npsql homepage:

I had the same problem on empty database. Before legacy timestamp behavior applied : base(options) is called And the base class constructor seeds the default data and throws the exception image

1reaction
tidyuicommented, Dec 1, 2021

We’ll take a look if we can add default configuration in our extension methods to avoid this problem!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot write DateTime with Kind=UTC to PostgreSQL type ...
A. Solved by adding. AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); to the Startup Configure method.
Read more >
Datetime error with .NET 6 and PostgreSQL - Duong's Blog
It says that you've asked Npgsql to send an Unspecified DateTime as a PG timestamptz type, e.g. by setting NpgsqlDbType.
Read more >
15: B.2. Handling of Invalid or Ambiguous Timestamps
Ordinarily, if a date/time string is syntactically valid but contains out-of-range field values, an error will be thrown. For example, input specifying the ......
Read more >
5.5. Date/Time Types
Table 5-10 shows some possible inputs for the date type. ... Note: If an invalid time zone is specified, the time zone becomes...
Read more >
PostgreSQL: Documentation: 8.1: Date/Time Key Words
The key word ABSTIME is ignored for historical reasons: In very old releases of PostgreSQL, invalid values of type abstime were emitted as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found