Using Entity Framework Core but not sure how to store a DateTime?
If you rely on converting a DateTime to a different timezone, one of the best ways is to store the DateTime as UTC in the database. That way, you can decide which timezone to display the DateTime in your application as you know you will always be converting it from UTC.
And there is an easy way to do this in EF Core. You can create a ValueConverter that allows you to transform a value when saving it to the database and replicate that when getting the value from the database to the application.
And the great thing about ValueConverter's is that it's possible to apply it in a DbContext to all properties of a particular type.
Watch my video where I create a DateTimeUtcConverter class that saves the time as UTC in the database, and then converts it back to the local timezone in the application.
Round The Code
Using Entity Framework Core but not sure how to store a DateTime?
If you rely on converting a DateTime to a different timezone, one of the best ways is to store the DateTime as UTC in the database. That way, you can decide which timezone to display the DateTime in your application as you know you will always be converting it from UTC.
And there is an easy way to do this in EF Core. You can create a ValueConverter that allows you to transform a value when saving it to the database and replicate that when getting the value from the database to the application.
And the great thing about ValueConverter's is that it's possible to apply it in a DbContext to all properties of a particular type.
Watch my video where I create a DateTimeUtcConverter class that saves the time as UTC in the database, and then converts it back to the local timezone in the application.
1 week ago | [YT] | 6