title: Entity Framework always save DateTimeOffset as UTC
body: <p>Is there a way that I can instruct Entity Framework to always store DateTimeOffset as the UTC value.  Technically there is no reason for this but I prefer it if all the data in my database is consistent.</p>
  
  <p>Currently it is storing the DateTimeOffset as received from the client which could be anything depending on the users locale.</p>
  
  <p>I was thinking of perhaps intercepting all the SaveChanges method, looping through the changes, looking for DateTimeOffset types and doing the conversion explicitly, but this seems like a lot of work which could be mitigated if EntityFramework has something built in.</p>
  
  <p>Any insight or suggestions would be greatly appreciated.</p>
  
  <p><strong>EDIT:</strong> The difference between my post and others dealing with this topic is that my C# property is of type DateTimeOffset and my database field is also DateTimeOffset(0).  There is not technical reason to convert other than consistency.  If I store a value as "17:00 +5:00" or "10:00 -2:00" it doesn't matter, those are both identical moments in time.  However, I would like to have all my dates stored as "12:00 +0:00" so that it is easier to read in SSMS or other DB tools.</p>
  
  <p><strong>EDIT 2:</strong> Seeing as there doesn't seem to be "simple" way to do this I am looking to now tackle the problem in the DbContext.SaveChanges() function. The problem is that no matter what I do, the value won't change?  Here's the code so far:</p>
  
  <pre><code>public new void SaveChanges()
  {
      foreach (var entry in this.ChangeTracker.Entries())
      {
          foreach (var propertyInfo in entry.Entity.GetType().GetProperties().Where(p =&gt; p.CanWrite &amp;&amp; p.PropertyType == typeof(DateTimeOffset)))
          {
              var value = entry.CurrentValues.GetValue&lt;DateTimeOffset&gt;(propertyInfo.Name);
  
              var universalTime = value.ToUniversalTime();
              entry.CurrentValues[propertyInfo.Name] = universalTime;
              // entry.Property(propertyInfo.Name).CurrentValue = universalTime;
  
              // Adding a watch to "entry.CurrentValues[propertyInfo.Name]" reveals that it has not changed??
          }
  
          // TODO: DateTimeOffset?
      }
  
      base.SaveChanges();
  }
  </code></pre>
  
  <p><strong>EDIT 3</strong> I finally managed to solve this, see answer below</p>