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)
-
0
Make the changes not tracked by applying
.AsNoTracking()
to your LINQ query or mappingentity
to a new instance. -
0
otherwise, you can map the entity into DTO and work on it.
-
0
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.