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
相关产品推荐
相关产品推荐

