diff --git a/src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertQueryContext.cs b/src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertQueryContext.cs index b4b200d2..2dfb3f8a 100644 --- a/src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertQueryContext.cs +++ b/src/Infrastructure/Masa.Alert.EntityFrameworkCore/AlertQueryContext.cs @@ -99,6 +99,30 @@ protected override void OnModelCreatingExecuting(ModelBuilder builder) b.ToView(AlertConsts.DB_TABLE_PREFIX + "WebHooks", AlertConsts.DB_SCHEMA); }); + // Apply provider-specific configurations + ApplyProviderSpecificConfigurations(builder); + base.OnModelCreatingExecuting(builder); } + + private void ApplyProviderSpecificConfigurations(ModelBuilder builder) + { + if (Database.ProviderName == "Npgsql.EntityFrameworkCore.PostgreSQL") + { + foreach (var entityType in builder.Model.GetEntityTypes()) + { + foreach (var property in entityType.GetProperties()) + { + if (property.ClrType == typeof(DateTime)) + { + property.SetValueConverter(new DateTimeUtcConverter()); + } + else if (property.ClrType == typeof(DateTime?)) + { + property.SetValueConverter(new NullableDateTimeUtcConverter()); + } + } + } + } + } } diff --git a/src/Infrastructure/Masa.Alert.EntityFrameworkCore/_Imports.cs b/src/Infrastructure/Masa.Alert.EntityFrameworkCore/_Imports.cs index 3035a047..d272b800 100644 --- a/src/Infrastructure/Masa.Alert.EntityFrameworkCore/_Imports.cs +++ b/src/Infrastructure/Masa.Alert.EntityFrameworkCore/_Imports.cs @@ -13,6 +13,7 @@ global using Masa.Alert.Domain.WebHooks.Aggregates; global using Masa.Alert.Domain.WebHooks.Repositories; global using Masa.Alert.Infrastructure.EntityFrameworkCore.EntityFrameworkCore.ValueConverters; +global using Masa.Alert.Infrastructure.EntityFrameworkCore.ValueConverters; global using Masa.BuildingBlocks.Data.UoW; global using Masa.BuildingBlocks.Data.Contracts; global using Masa.Contrib.Ddd.Domain.Repository.EFCore; diff --git a/src/Infrastructure/Masa.Alert.Infrastructure.EntityFrameworkCore/EntityFrameworkCore/ValueConverters/DateTimeUtcConverter.cs b/src/Infrastructure/Masa.Alert.Infrastructure.EntityFrameworkCore/EntityFrameworkCore/ValueConverters/DateTimeUtcConverter.cs new file mode 100644 index 00000000..c2beca0b --- /dev/null +++ b/src/Infrastructure/Masa.Alert.Infrastructure.EntityFrameworkCore/EntityFrameworkCore/ValueConverters/DateTimeUtcConverter.cs @@ -0,0 +1,13 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the Apache License. See LICENSE.txt in the project root for license information. + +namespace Masa.Alert.Infrastructure.EntityFrameworkCore.ValueConverters; + +public class DateTimeUtcConverter : ValueConverter +{ + public DateTimeUtcConverter() : base( + v => v.ToUniversalTime(), + v => DateTime.SpecifyKind(v, DateTimeKind.Utc)) + { + } +} \ No newline at end of file diff --git a/src/Infrastructure/Masa.Alert.Infrastructure.EntityFrameworkCore/EntityFrameworkCore/ValueConverters/NullableDateTimeUtcConverter.cs b/src/Infrastructure/Masa.Alert.Infrastructure.EntityFrameworkCore/EntityFrameworkCore/ValueConverters/NullableDateTimeUtcConverter.cs new file mode 100644 index 00000000..8366647d --- /dev/null +++ b/src/Infrastructure/Masa.Alert.Infrastructure.EntityFrameworkCore/EntityFrameworkCore/ValueConverters/NullableDateTimeUtcConverter.cs @@ -0,0 +1,13 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the Apache License. See LICENSE.txt in the project root for license information. + +namespace Masa.Alert.Infrastructure.EntityFrameworkCore.ValueConverters; + +public class NullableDateTimeUtcConverter : ValueConverter +{ + public NullableDateTimeUtcConverter() : base( + v => v.HasValue ? v.Value.Kind == DateTimeKind.Unspecified ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc).ToUniversalTime() : v.Value.ToUniversalTime() : null, + v => v.HasValue ? DateTime.SpecifyKind(v.Value, DateTimeKind.Utc) : null) + { + } +}