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

[BUG] [EF Core] Postgres Error in .NET 6, Cannot write DateTime with Kind=Local to PostgreSQL type #6357

Closed
dust63 opened this issue Jan 5, 2022 · 2 comments · Fixed by #6426
Assignees
Milestone

Comments

@dust63
Copy link

dust63 commented Jan 5, 2022

  • Your Abp package version: 7.0.0
  • Your base framework: .Net Framework or .Net Core: .NET 6.0.101
  • Steps needed to reproduce the problem: using EF Core with postgres SQL, Npgsql.EntityFrameworkCore.PostgreSQL 6.0.2 (last version)
  • Exception message and stack trace if available.

Stack Trace:

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (81ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT a."Id", a."CreationTime", a."CreatorUserId", a."DeleterUserId", a."DeletionTime", a."DisplayName", a."IsDeleted", a."LastModificationTime", a."LastModifierUserId", a."Name"
      FROM "AbpEditions" AS a
      WHERE a."Name" = 'Standard'
      LIMIT 1
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (8ms) [Parameters=[@p0='?' (DbType = DateTime), @p1='?' (DbType = Int64), @p2='?' (DbType = Int64), @p3='?' (DbType = DateTime), @p4='?', @p5='?' (DbType = Boolean), @p6='?' (DbType = DateTime), @p7='?' (DbType = Int64), @p8='?'], CommandType='Text', CommandTimeout='30']
      INSERT INTO "AbpEditions" ("CreationTime", "CreatorUserId", "DeleterUserId", "DeletionTime", "DisplayName", "IsDeleted", "LastModificationTime", "LastModifierUserId", "Name")
      VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8)
      RETURNING "Id";
fail: Microsoft.EntityFrameworkCore.Update[10000]
      An exception occurred in the database while saving changes for context type 'Sparklight.Flux.EntityFrameworkCore.FluxDbContext'.
      Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
       ---> System.InvalidCastException: Cannot write DateTime with Kind=Local to PostgreSQL type 'timestamp with time zone', only UTC is supported. Note that it's not possible to mix DateTimes with different Kinds in an array/range. See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior.
         at Npgsql.Internal.TypeHandlers.DateTimeHandlers.TimestampTzHandler.ValidateAndGetLength(DateTime value, NpgsqlParameter parameter)
         at Npgsql.Internal.TypeHandlers.DateTimeHandlers.TimestampTzHandler.ValidateObjectAndGetLength(Object value, NpgsqlLengthCache& lengthCache, NpgsqlParameter parameter)

I can fix the problem for now by following this stackoverflow

Add into the Startup.cs -> Configure:

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

@dust63
Copy link
Author

dust63 commented Jan 5, 2022

Notice that I follow your procedure

@ismcagdas ismcagdas added this to the v7.1 milestone Jan 6, 2022
@ismcagdas
Copy link
Member

@dust63 thanks, we will check it.

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

Successfully merging a pull request may close this issue.

2 participants