我的上下文:我需要创建一个使用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:
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脚本是
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脚本映射正确的数据类型。我希望你能理解我的需要(你可以要求我澄清)。
发布于 2022-09-11 03:35:23
它是bytea和timestamp without time zone
发布于 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实例的代码。有关更多信息,请参见医生们和这篇博客文章。
发布于 2022-10-01 15:27:21
对于timestamp without time zone,可以在创建表达式中使用timestamp。
对于imestamp with time zone,可以在创建表达式中使用timestamptz。
https://stackoverflow.com/questions/73676567
复制相似问题