首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从SQLite移植到PostgreSQL:在PostgreSQL 14.5 SQL脚本中,.NET 6中的数据类型byte[]和DateTime是什么?

从SQLite移植到PostgreSQL:在PostgreSQL 14.5 SQL脚本中,.NET 6中的数据类型byte[]和DateTime是什么?
EN

Stack Overflow用户
提问于 2022-09-11 03:16:09
回答 3查看 67关注 0票数 0

我的上下文:我需要创建一个使用ASP.NET Core WebAPI .NET 6、Entity Framework 6、PostgreSQL 14.5和JWT身份验证/授权的web应用程序。我在https://github.com/patrickgod/AuthenticationWebApi模仿一个样本,然后分叉给我https://github.com/donhuvy/AuthenticationWebApi

此源代码使用.NET 6、SQLite、6、HMACSHA512:

代码语言:javascript
复制
namespace AuthenticationWebApi.Models

{
    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; } = string.Empty;
        public byte[] PasswordHash { get; set; } = new byte[32]; // <-- In don't know how to choose according datatype in PostgreSQL' DDL script.
        public byte[] PasswordSalt { get; set; } = new byte[32]; // <-- In don't know how to choose according datatype in PostgreSQL' DDL script.
        public string RefreshToken { get; set; } = string.Empty;
        public DateTime TokenCreated { get; set; } // <-- In don't know how to choose according datatype in PostgreSQL' DDL script.
        public DateTime TokenExpires { get; set; } // <-- In don't know how to choose according datatype in PostgreSQL' DDL script.
        public string Role { get; set; } = string.Empty;
    }
}

https://github.com/donhuvy/AuthenticationWebApi/blob/master/AuthenticationWebApi/Models/User.cs

我需要迁移到PostgreSQL 14.6。我的错误SQL脚本是

代码语言:javascript
复制
CREATE TABLE public."my_user"
(
    id integer,
    username character varying(32),
    password_hash character varying(32), // <-- Incorrect mapping.
    password_salt character varying(32), // <-- Incorrect mapping.
    refresh_token character varying(32),
    token_created time with time zone, // <-- Incorrect mapping.
    token_expires time with time zone, // <-- Incorrect mapping.
    role text
);

ALTER TABLE IF EXISTS public."my_user"
    OWNER to postgres;

my_user数据库系统中,我使用表名user来避免与保留关键字user的重复。

帮助我在PostgreSQL中为SQL脚本映射正确的数据类型。我希望你能理解我的需要(你可以要求我澄清)。

EN

回答 3

Stack Overflow用户

发布于 2022-09-11 03:35:23

它是byteatimestamp without time zone

https://www.npgsql.org/doc/types/basic.html

票数 0
EN

Stack Overflow用户

发布于 2022-09-13 06:51:10

如前所述,.NET byte[]映射到PostgreSQL bytea

对于DateTime,这取决于: UTC时间戳在PG中由timestamp with time zone表示,而其他时间戳(在某些隐式本地时区或未知时区中)则由timestamp without time zone表示。Npgsql在.NET中强制执行这一区别,只接受DateTime和Kind=UTC的timestamp with time zone,而只接受非UTC的timestamp with time zone。因此,这是一个要保存什么样的时间戳数据的问题,它会影响您将用于向Npgsql提供DateTime实例的代码。有关更多信息,请参见医生们这篇博客文章

票数 0
EN

Stack Overflow用户

发布于 2022-10-01 15:27:21

对于timestamp without time zone,可以在创建表达式中使用timestamp

对于imestamp with time zone,可以在创建表达式中使用timestamptz

docs写映射PostgreSQL时间戳与时间戳

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73676567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档