You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LinqToDB+PostgreSQL下DateTime写入timestamp without time zone报错求助

解决方案

1. 配置LinqToDB的PostgreSQL时区处理策略

初始化数据库连接时,设置PostgreSQLOptions的DateTimeZoneHandling为Unspecified,禁止LinqToDB自动转换DateTime的Kind:

var dataConn = new DataConnection(
    new PostgreSQLDataProvider(
        new PostgreSQLOptions
        {
            DateTimeZoneHandling = DateTimeZoneHandling.Unspecified
        }),
    "你的数据库连接字符串");

此设置会保留DateTime原本的Kind值,避免自动转为UTC,适配timestamp without time zone字段的写入规则。

2. 手动将DateTime转为Unspecified Kind

由于timestamp without time zone不存储时区信息,直接将DateTime的Kind设置为Unspecified后再赋值给实体字段,可避免Npgsql驱动的兼容性问题:

tableEntity.DT = DateTime.SpecifyKind(dt, DateTimeKind.Unspecified);

3. 调整实体字段的DataType配置

修改实体字段的DataType为DataType.DateTime2,同时保留DbType为timestamp without time zone,修正LinqToDB的默认类型映射逻辑:

[Column("dt", DbType = "timestamp without time zone", DataType = DataType.DateTime2)]
public DateTime DT { get; set; }

内容的提问来源于stack exchange,提问作者VladimirK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 09:40:22