Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10.0 PostgreSql InvalidCastException with DateTime #1762

Closed
DjeeBay opened this issue Nov 13, 2021 · 6 comments
Closed

10.0 PostgreSql InvalidCastException with DateTime #1762

DjeeBay opened this issue Nov 13, 2021 · 6 comments
Assignees

Comments

@DjeeBay
Copy link

DjeeBay commented Nov 13, 2021

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 :
npgsql/efcore.pg#2000
https://www.npgsql.org/efcore/release-notes/6.0.html

Hope it helps.

@tidyui
Copy link
Member

tidyui commented Nov 28, 2021

Ping @junderhill

@tidyui
Copy link
Member

tidyui commented Dec 1, 2021

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

@tidyui tidyui added this to the Version 10.0.3 milestone Feb 8, 2022
@tidyui tidyui added this to To do in Version 10.1 Feb 22, 2022
@tidyui tidyui added this to To do in Version 10.1 Feb 22, 2022
@tidyui tidyui closed this as completed in a25be57 Mar 29, 2022
Version 10.1 automation moved this from To do to Done Mar 29, 2022
@tidyui tidyui modified the milestones: Version 10.1, Version 10.0.3 Mar 31, 2022
@tidyui tidyui removed this from Done in Version 10.1 Mar 31, 2022
@tidyui tidyui self-assigned this Mar 31, 2022
@PizzaConsole
Copy link

@tidyui FYI this commit did not fix the error for me. I still had to modify my Program.cs file (I have not migrated to a flat file)

        public static void Main(string[] args)
        {
            var test = CreateHostBuilder(args).Build();
            AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
            test.Run();
        }

@tidyui
Copy link
Member

tidyui commented Apr 13, 2022

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

To do this and revert to the legacy timestamp behavior, add the following to your context's constructor, before any Npgsql or EF Core operations are invoked:

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

@tzaorski
Copy link

tzaorski commented Jul 5, 2022

I had a similar problem. After upgrading the CMS to the latest version and adding the above workaround, the home page does not work (HTTP ERROR 404). The manager works without a problem.

@gyfke
Copy link
Contributor

gyfke commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

5 participants