4

Is there any way to specify Entity Framework to convert all DateTime in UTC (for azure database storage) ?

I would like that my model stay in local date time. I don't want to duplicate model datetime properties to have one in UTC (for EF) and another one in local (for my views, not mapped in EF).

Corey Adler
  • 15,539
  • 18
  • 64
  • 80
ZeBobo5
  • 334
  • 4
  • 14
  • You could write a custom saving changes method which uses reflection to find all datetime properties in objects that are going to be saved and convert them there... – Mansfield Nov 07 '13 at 15:42
  • Yes, that's ok for adding or updating to DB but not while reading from DB... – ZeBobo5 Nov 07 '13 at 15:46
  • 1
    Similar problem to http://stackoverflow.com/a/19377226/150342 – Colin Nov 07 '13 at 16:40

2 Answers2

0

That's not directly possible. Either you'll live without it or you'll have to create some "helper" code (properties) in your classes/entities.

cincura.net
  • 4,082
  • 15
  • 38
0

Take a look at DateTimeOffset introduced in .Net 4.5 - this stores timezone information as the datetimeoffset SQL type (both SQL Server 2008+ and Azure Database Storage).

By using DateTimeOffset, you do not need to worry anywhere near as much about time zones, which should let you still work in local time for your model.

Matt DeKrey
  • 11,212
  • 5
  • 52
  • 67
  • Actually `DateTimeOffset` does not store time zone information, it stores an offset from UTC. It gives some derived information but you can't get time zone information from a `DateTimeOffset` value, so it is of limited use. – Suncat2000 Apr 22 '16 at 19:16