pgsql时间问题 返回

老大,PGSQL时间插入问题有没有时间帮忙看看了
实体有个基类定义了cre_time,类型为datetimeoffset,并给默认值,数据库字段类型为timestamp without time zone
[SugarColumn(IsNullable =true,ColumnDataType ="timestamp")]
public DateTimeOffset? cre_time { get; set; } = new DateTimeOffset(DateTime.Now.ToUniversalTime());
其它实体都继承这个基类,在插入之前实体的cre_time值是一样的(在commit前断点),但是插入后,时间就不同了,如图片所示。
var list=new List<AsnDetailEntity>();
var asn=new AsnEntity();
await DbContext.AsTenant().BrginTranAsync()
await DbContext.AsTenant().InsertableWithAttr(list).ExecuteCommandIdentityIntoEntityAsync(); 此句插入数据库会加8个小时转为本地时间,
await DbContext.AsTenant().InsertableWithAttr(asn).ExecuteCommandAsync(); 此句插入数据库没有加8个小时,还是UTC时间
await DbContext.AsTenant().CommitTranAsync();
如果打开以下两个开关,就都正常了,但是项目原因不能打开这个开关
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
热忱回答(7)
-
奔跑的蜗牛 VIP0
2024/12/27INSERT INTO "biz"."asn_detail" ("asn_no","seq","co_num","co_line","cus_po","item","item_name","unit","cus_item","co_order_qty","co_open_qty","asn_qty","shipping_qty","coitem_rid","whse","uf_whse","lot_qty","p_m_t_code","ref_type","ref_num","ref_line","cre_time","upd_time","createdby","updatedby") VALUES
( '202412270022' , '1' , 'COA0000398' , '17' , '415014811' , 'MS-SPMC000014-B' , NULL , 'PCS' , 'M2.0 Screw' , '20000' , '20000' , '20000' , '0' , '20260f03-8b3a-48f4-89a3-e521a288a658' , 'WH' , '包材仓' , '0' , 'P' , 'P' , NULL , '0' ,'2024-12-27T06:57:25.2664177+00:00', NULL , 'gliu' , NULL ),
( '202412270022' , '2' , 'COA0000398' , '18' , '415014811' , 'EA-FD0320FP0002-C' , NULL , 'PCS' , 'Full range driver' , '32900' , '32900' , '32900' , '0' , '7cdee86e-a1b8-48bc-91e3-5ae9ab1e9628' , 'WH' , '五金仓' , '0' , 'P' , 'P' , NULL , '0' ,'2024-12-27T06:57:25.3135353+00:00', NULL , 'gliu' , NULL )
;
INSERT INTO "biz"."asn"
("asn_no","customer_no","customer_name","billdate","site","status","cre_time","upd_time","createdby","updatedby")
VALUES
(N'202412270022',N'TSASI11',N'Sky Light Imaging Limited','2024-12-27 00:00:00.000',N'TSAT',N'U',N'2024/12/27 6:57:25 +00:00',null,N'gliu',null) returning "asn_no"
多条和单条生的语句不同,单条会在时间前加N''
0 回复 -
fate sta VIP0
2024/12/27if (StaticConfig.AppContext_ConvertInfinityDateTime == false)
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
上面源码中默认是true,想改成false
StaticConfig.AppContext_ConvertInfinityDateTime=false;
0 回复 -
fate sta VIP0
2024/12/27打印SQL用这个方法,这个输出才是准确的
//获取原生SQL推荐 5.1.4.63 性能OK
Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
0 回复 -
奔跑的蜗牛 VIP0
2024/12/27@fate sta:就是要false了,不能打开这两个设置。前边就是因为打开了,造成别的同事的功能中日期都出错了
0 回复 -
奔跑的蜗牛 VIP0
2024/12/27输出的sql如下
0 回复 -
奔跑的蜗牛 VIP0
2024/12/27@奔跑的蜗牛:应该就是参数化插入,又做了转换
0 回复 -
fate sta VIP0
2024/12/280 回复