Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DateTime, DateTime>
{
public DateTimeUtcConverter() : base(
v => v.ToUniversalTime(),
v => DateTime.SpecifyKind(v, DateTimeKind.Utc))
{
}
}
Original file line number Diff line number Diff line change
@@ -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<DateTime?, DateTime?>
{
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)
{
}
}