How to write DateTime by using LinqToDB in Postgres DB field 'timestamp without time zone'?

246 Views Asked by At

C# + Linq2db + Postgres

entity for table

[Table("my_table", Schema = "myScheme")]
public class TableEntity 
... [Column("dt", DbType = "timestamp without time zone", DataType = DataType.DateTime)]
public DateTime DT { get; set; }...

I set value of the entity

tableEntity.DT = DateTime.SpecifyKind(someDate, DateTimeKind.Local);
//In debugger I see, that here Kind for tableEntity.DT is DateTimeKind.Local

next row start LinqToDb command:

dataConn.InsertWithInt32IdentityAsync(loanContractEntity);

but underhood linqToDb convert DateTimeKind for tableEntity.DT from Local to UTC and then throw an exception: Cannot write DateTime with Kind=UTC to PostgreSQL type 'timestamp without time zone', consider using 'timestamp with time zone'. 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.

But I need use 'timestamp without time zone'.

So - How to write C# DateTime by using LinqToDB in Postgres DB field 'timestamp without time zone'?

before link command (the same rezult would be if kind were "Local")

after linq2DB command

1

There are 1 best solutions below

1
Mubeen Siddiqui On

Use this, this will surely solve your problem. tableEntity.DT = DateTime.SpecifyKind(someDate, DateTimeKind.Unspecified);