EF Core 3.0, Datetime timezone issue C#

2.4k views Asked by At

The datetime stored in the DB(postgresql) is with time zone(EST), while binding the value to code it is getting converted to system local time(IST), Even while trying to fetch it as string using convertors the value get converted to local time and display at string

Please help on how to ignore the Timezone in the ModelBuilder

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<TblData>(entity =>
    {

        var converter = new ValueConverter<DateTime, DateTime>(
                   v => new DateTime(),
                   v => DateTimeOffset.Parse(v.ToString()).DateTime);

        var converter1 = new ValueConverter<string, DateTime>(
                   v => new DateTime(),
                   v => v.ToString());

        entity.Property(e => e.StartTime)
                               .HasColumnName("start_time")
                               .HasColumnType("timestamp(4) with time zone");
                               .HasConversion(converter);

        //both converters return value in localtime only
    }
}

a zone specific time conversion is not possible as the API layer is dealing with multiple timezone DB's and API is deployed in both Linux and windows server.

Please help !!!

1

There are 1 answers

0
Rui Lima On

I usually set the TZ of the db user.

ALTER ROLE my_db_user IN DATABASE my_database
    SET "TimeZone" TO 'UTC';

Will this be of any use?