gpt4 book ai didi

Have a DateTime Kind.Unspecified column in Postgresql in EF Core(在EF Core中的PostgreSQL中有一个DateTime Kind。未指定列)

转载 作者:bug小助手 更新时间:2023-10-24 21:13:07 25 4
gpt4 key购买 nike



I have this database column:

我有一个数据库列:


[MaxLength(10)]
[Required]
public List<NpgsqlRange<System.DateTime>> Dates { get; set; }

How do I make this allow only Kind.Unspecified?

如何使其仅允许Kind。未指定?


I get this error when I insert:

当我插入以下内容时,出现以下错误:



System.InvalidCastException : Cannot write DateTime with
Kind=Unspecified 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.



Every single solution I have seen is to use Npgsql.EnableLegacyTimestampBehavior. However, that is to allow unspecified type in a UTC column, and I don't want to allow unspecified type in a UTC column. I want the column to be strongly typed as unspecified.

我见过的每个解决方案都是使用Npgsql.EnableLegacyTimestampBehavior。但是,这是为了在UTC列中允许未指定的类型,而我不希望在UTC列中允许未指定的类型。我希望该列被强类型化为未指定。


Also, when I use EnableLegacyTimestampBehavior, it saves it to postgresql with the "+12" in it, which IS UTC (which I don't want).

此外,当我使用EnableLegacyTimestampBehavior时,它会将其保存为带有“+12”的PostgreSQL,即UTC(我不想要)。


更多回答

What is your field type in DB?

您在数据库中的字段类型是什么?

@Orifjon Dates = table.Column<List<NpgsqlRange<System.DateTime>>>(type: "tstzmultirange", maxLength: 10, nullable: true)

@ORIFJON DATES=table.Column>>(type:“tstzMultirange”,最大长度:10,可空:TRUE)

优秀答案推荐

It seems that, your database type does not match your code type here. Database type timestamp without time zone corresponds to DateTime(Kind=Unspecified) and timestamp with time zone corresponds to DateTime(Kind=UTC). for range and multirange types, these valuers cannot be mixed.

似乎,您的数据库类型与您这里的代码类型不匹配。没有时区的数据库类型TIMESTAMP对应于DATETIME(KIND=未指定),而具有时区的TIMESTAMP对应于DATETIME(KIND=UTC)。对于范围和多范围类型,这些赋值器不能混合使用。


If you want to keep C# type, you can change
Dates = table.Column<List<NpgsqlRange<System.DateTime>>>(type: "tstzmultirange", maxLength: 10, nullable: true) to Dates = table.Column<List<NpgsqlRange<System.DateTime>>>(type: "tsmultirange", maxLength: 10, nullable: true)

如果您想保留C#类型,可以将DATES=table.Column >>(type:“tstzMultirange”,max Length:10,Null able:True)更改为Dates=table.Column >>(type:“tsMultirange”,MaxLength:10,Null able:True)


Since you can get different kind of DateTime, you can explicitly create new DateTime instance with Kind=Unspecified before assigning it to Dates property.

因为您可以获得不同类型的DateTime,所以在将其分配给Dates属性之前,您可以显式地创建新的DateTime实例,其Kind=UNSPECIFIED。


For more details, See: Type mapping

有关详情,请参阅:类型映射


Postgresql built in types

PostgreSQL内置类型


p.s.: You can also use NodaTime which aligns better with Postgresql types.

附注:您也可以使用NodaTime,它与PostgreSQL类型更一致。


更多回答

Thanks. That code I commented was generated. How do I change what is generated? The code in the question is the only place I defined the type.

谢谢。我注释的代码是生成的。如何更改生成的内容?问题中的代码是我定义该类型的唯一位置。

It is generated based on your actual database type. If you can't change database type, you have to go with DateTime(Kind=UTC).

它是根据实际的数据库类型生成的。如果不能更改数据库类型,则必须使用DATETIME(KIND=UTC)。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com