Browse Source

Fix the `AbpDateTimeValueConverter `.

https://github.com/dotnet/efcore/issues/34760
pull/20803/head
maliming 1 year ago
parent
commit
80e02d37dc
  1. 10
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 17
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/AbpDateTimeValueConverter.cs
  3. 5
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/ValueConverters/EFCore_DateTimeKindTests.cs
  4. 2
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Serializer/MongoDB_DateTimeKind_Tests.cs
  5. 2
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DateTimeKind_Tests.cs

10
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

@ -37,7 +37,6 @@ using Volo.Abp.Reflection;
using Volo.Abp.Timing;
using Volo.Abp.Uow;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Volo.Abp.EntityFrameworkCore;
@ -773,8 +772,6 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
protected virtual void ConfigureValueConverter<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
where TEntity : class
{
//TODO: There is a exception in EF Core 9, I'm trying to find a workaround.
return;
if (mutableEntityType.BaseType == null &&
!typeof(TEntity).IsDefined(typeof(DisableDateTimeNormalizationAttribute), true) &&
!typeof(TEntity).IsDefined(typeof(OwnedAttribute), true) &&
@ -785,6 +782,9 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
return;
}
AbpDateTimeValueConverter.Clock = Clock;
AbpNullableDateTimeValueConverter.Clock = Clock;
foreach (var property in mutableEntityType.GetProperties().
Where(property => property.PropertyInfo != null &&
(property.PropertyInfo.PropertyType == typeof(DateTime) || property.PropertyInfo.PropertyType == typeof(DateTime?)) &&
@ -795,8 +795,8 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
.Entity<TEntity>()
.Property(property.Name)
.HasConversion(property.ClrType == typeof(DateTime)
? new AbpDateTimeValueConverter(Clock)
: new AbpNullableDateTimeValueConverter(Clock));
? new AbpDateTimeValueConverter()
: new AbpNullableDateTimeValueConverter());
}
}
}

17
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/ValueConverters/AbpDateTimeValueConverter.cs

@ -1,5 +1,4 @@
using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Volo.Abp.Timing;
@ -7,10 +6,12 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters;
public class AbpDateTimeValueConverter : ValueConverter<DateTime, DateTime>
{
public AbpDateTimeValueConverter(IClock clock, ConverterMappingHints? mappingHints = null)
public static IClock? Clock { get; set; }
public AbpDateTimeValueConverter(ConverterMappingHints? mappingHints = null)
: base(
x => clock.Normalize(x),
x => clock.Normalize(x),
x => Clock!.Normalize(x),
x => Clock!.Normalize(x),
mappingHints)
{
}
@ -18,10 +19,12 @@ public class AbpDateTimeValueConverter : ValueConverter<DateTime, DateTime>
public class AbpNullableDateTimeValueConverter : ValueConverter<DateTime?, DateTime?>
{
public AbpNullableDateTimeValueConverter(IClock clock, ConverterMappingHints? mappingHints = null)
public static IClock? Clock { get; set; }
public AbpNullableDateTimeValueConverter(ConverterMappingHints? mappingHints = null)
: base(
x => x.HasValue ? clock.Normalize(x.Value) : x,
x => x.HasValue ? clock.Normalize(x.Value) : x,
x => x.HasValue ? Clock!.Normalize(x.Value) : x,
x => x.HasValue ? Clock!.Normalize(x.Value) : x,
mappingHints)
{
}

5
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/ValueConverters/EFCore_DateTimeKindTests.cs

@ -11,7 +11,7 @@ namespace Volo.Abp.EntityFrameworkCore.ValueConverters;
public abstract class EFCore_DateTimeKindTests : DateTimeKind_Tests<AbpEntityFrameworkCoreTestModule>
{
[Fact(Skip = "Skip temporarily")]
[Fact]
public async Task DateTime_Kind_Should_Be_Normalized_In_View_Query_Test()
{
var personName = "bob lee";
@ -37,6 +37,7 @@ public abstract class EFCore_DateTimeKindTests : DateTimeKind_Tests<AbpEntityFra
}
}
[Collection(nameof(EFCore_DateTimeKindTests))]
public class DateTimeKindTests : EFCore_DateTimeKindTests
{
protected override void AfterAddApplication(IServiceCollection services)
@ -46,6 +47,7 @@ public class DateTimeKindTests : EFCore_DateTimeKindTests
}
}
[Collection(nameof(EFCore_DateTimeKindTests))]
public class DateTimeKindTests_Local : EFCore_DateTimeKindTests
{
protected override void AfterAddApplication(IServiceCollection services)
@ -55,6 +57,7 @@ public class DateTimeKindTests_Local : EFCore_DateTimeKindTests
}
}
[Collection(nameof(EFCore_DateTimeKindTests))]
public class DateTimeKindTests_Utc : EFCore_DateTimeKindTests
{
protected override void AfterAddApplication(IServiceCollection services)

2
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Serializer/MongoDB_DateTimeKind_Tests.cs

@ -95,7 +95,7 @@ public class DisableDateTimeKindTests : TestAppTestBase<AbpMongoDbTestModule>
base.AfterAddApplication(services);
}
[Fact(Skip = "Skip temporarily")]
[Fact]
public async Task DateTime_Kind_Should_Be_Normalized_By_MongoDb_Test()
{
var personId = Guid.NewGuid();

2
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/DateTimeKind_Tests.cs

@ -18,7 +18,7 @@ public abstract class DateTimeKind_Tests<TStartupModule> : TestAppTestBase<TStar
PersonRepository = GetRequiredService<IPersonRepository>();
}
[Fact(Skip = "Skip temporarily")]
[Fact]
public async Task DateTime_Kind_Should_Be_Normalized_Test()
{
var personId = Guid.NewGuid();

Loading…
Cancel
Save