using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using System; namespace Timeline.Entities { // Copied from https://github.com/dotnet/efcore/issues/4711#issuecomment-589842988 public static class UtcDateAnnotation { private const string IsUtcAnnotation = "IsUtc"; private static readonly ValueConverter UtcConverter = new ValueConverter(v => v, v => DateTime.SpecifyKind(v, DateTimeKind.Utc)); public static PropertyBuilder IsUtc(this PropertyBuilder builder, bool isUtc = true) => builder.HasAnnotation(IsUtcAnnotation, isUtc); public static bool IsUtc(this IMutableProperty property) => ((bool?)property.FindAnnotation(IsUtcAnnotation)?.Value) ?? true; /// /// Make sure this is called after configuring all your entities. /// public static void ApplyUtcDateTimeConverter(this ModelBuilder builder) { foreach (var entityType in builder.Model.GetEntityTypes()) { foreach (var property in entityType.GetProperties()) { if (!property.IsUtc()) { continue; } if (property.ClrType == typeof(DateTime)) { property.SetValueConverter(UtcConverter); } } } } } }