25 lines
872 B
C#
25 lines
872 B
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
|
|
namespace AMWD.Common.EntityFrameworkCore.Converters
|
|
{
|
|
/// <summary>
|
|
/// Defines the conversion from a nullable <see cref="DateOnly"/> object to a nullable <see cref="DateTime"/> which can be handled by the database engine.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// As of 2022-06-04 only required for Microsoft SQL server on .NET 6.0.
|
|
/// </remarks>
|
|
public class NullableDateOnlyConverter : ValueConverter<DateOnly?, DateTime?>
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="NullableDateOnlyConverter"/> class.
|
|
/// </summary>
|
|
public NullableDateOnlyConverter()
|
|
: base(
|
|
d => d == null ? null : new DateTime?(d.Value.ToDateTime(TimeOnly.MinValue)),
|
|
d => d == null ? null : new DateOnly?(DateOnly.FromDateTime(d.Value))
|
|
)
|
|
{ }
|
|
}
|
|
}
|