Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Timezone conversion on server side #6944


User avatar
0
Ricavir created

Hi,

Server : asp.net core Client : angular

I'm storing datetime properties with UTC timezone on database. On client side, timezone conversion works great.

My problem is on server side : I need to convert all datetime properties of an entity to the current tenant timezone. I've used refection to do that :

foreach (PropertyInfo property in entity.GetType().GetProperties().Where(p => p.PropertyType == typeof(DateTime?) || p.PropertyType == typeof(DateTime)))
{
    if (property.GetValue(entity) != null)
        property.SetValue(entity, _timeZoneConverter.Convert((property.GetValue(entity) as DateTime?).Value, tenantId));
}

When I run this code, all datetime properties are changed to local time as I want BUT stored in database at the end of UnitOfWork...

How can I do this conversion without changing datetime property values in database ?

PS : I know your existing example to export users but it doesn't fit my need


3 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Make the changes not tracked by applying .AsNoTracking() to your LINQ query or mapping entity to a new instance.

  • User Avatar
    0
    ryancyq created
    Support Team

    otherwise, you can map the entity into DTO and work on it.

  • User Avatar
    0
    Ricavir created

    Tks for your answers, I've choosen the mapping option. As I was on domain layer, I didn't had access to DTOs. Therefore, I've created an objet that inherits from the entity I wanted to convert and that works.