From 66b9deae515acbabe536387dfa4d7ec089b957d0 Mon Sep 17 00:00:00 2001 From: Alex Klaus Date: Mon, 1 Jan 2024 13:55:32 +1000 Subject: [PATCH] Got rid off DateOnly converters --- Api/Api.csproj | 4 ++- Database/DataContext.cs | 4 +-- Database/Infrastructure/DateOnlyComparer.cs | 29 ---------------- Database/Infrastructure/DateOnlyConverter.cs | 34 ------------------- .../20220322114144_InititalCreate.cs | 2 +- 5 files changed, 5 insertions(+), 68 deletions(-) delete mode 100644 Database/Infrastructure/DateOnlyComparer.cs delete mode 100644 Database/Infrastructure/DateOnlyConverter.cs diff --git a/Api/Api.csproj b/Api/Api.csproj index f9de729..9897cbf 100644 --- a/Api/Api.csproj +++ b/Api/Api.csproj @@ -7,8 +7,10 @@ - + + + diff --git a/Database/DataContext.cs b/Database/DataContext.cs index 18899b4..53672c0 100644 --- a/Database/DataContext.cs +++ b/Database/DataContext.cs @@ -1,5 +1,4 @@ using AK.DbSample.Database.Entities; -using AK.DbSample.Database.Infrastructure; using Microsoft.EntityFrameworkCore; @@ -20,8 +19,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { // Date is a DateOnly property and date on database builder.Property(x => x.Date) - // These converters are still required in EF 7 (https://github.com/dotnet/efcore/issues/24507) - .HasConversion(); + .HasColumnType("date"); // Set cascade delete builder.HasOne(p => p.Client) diff --git a/Database/Infrastructure/DateOnlyComparer.cs b/Database/Infrastructure/DateOnlyComparer.cs deleted file mode 100644 index 6b76fde..0000000 --- a/Database/Infrastructure/DateOnlyComparer.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.EntityFrameworkCore.ChangeTracking; - -namespace AK.DbSample.Database.Infrastructure; - -/// -/// Compares . -/// -public class DateOnlyComparer : ValueComparer -{ - /// - /// Creates a new instance of this converter. - /// - public DateOnlyComparer() : base( - (d1, d2) => d1 == d2 && d1.DayNumber == d2.DayNumber, - d => d.GetHashCode()) {} -} - -/// -/// Compares . -/// -public class NullableDateOnlyComparer : ValueComparer -{ - /// - /// Creates a new instance of this converter. - /// - public NullableDateOnlyComparer() : base( - (d1, d2) => d1 == d2 && d1.GetValueOrDefault().DayNumber == d2.GetValueOrDefault().DayNumber, - d => d.GetHashCode()) {} -} \ No newline at end of file diff --git a/Database/Infrastructure/DateOnlyConverter.cs b/Database/Infrastructure/DateOnlyConverter.cs deleted file mode 100644 index 12925af..0000000 --- a/Database/Infrastructure/DateOnlyConverter.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -namespace AK.DbSample.Database.Infrastructure; - -/// -/// Converts to and vice versa. -/// -public class DateOnlyConverter : ValueConverter -{ - /// - /// Creates a new instance of this converter. - /// - public DateOnlyConverter() : base( - d => d.ToDateTime(TimeOnly.MinValue), - d => DateOnly.FromDateTime(d)) { } -} - -/// -/// Converts to and vice versa. -/// -public class NullableDateOnlyConverter : ValueConverter -{ - /// - /// Creates a new instance of this converter. - /// - public NullableDateOnlyConverter() : base( - d => d == null - ? null - : new DateTime?(d.Value.ToDateTime(TimeOnly.MinValue)), - d => d == null - ? null - : new DateOnly?(DateOnly.FromDateTime(d.Value))) - { } -} \ No newline at end of file diff --git a/Database/Migrations/20220322114144_InititalCreate.cs b/Database/Migrations/20220322114144_InititalCreate.cs index 3456329..f2f6cdd 100644 --- a/Database/Migrations/20220322114144_InititalCreate.cs +++ b/Database/Migrations/20220322114144_InititalCreate.cs @@ -28,7 +28,7 @@ protected override void Up(MigrationBuilder migrationBuilder) { Number = table.Column(type: "nvarchar(100)", nullable: false), ClientId = table.Column(type: "bigint", nullable: false), - Date = table.Column(type: "datetime", nullable: false), + Date = table.Column(type: "date", nullable: false), Amount = table.Column(type: "decimal(18,2)", nullable: false) }, constraints: table =>