29 lines
890 B
C#
29 lines
890 B
C#
#if NET6_0_OR_GREATER
|
|
|
|
using System;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
|
|
namespace AMWD.Common.EntityFrameworkCore.Converters
|
|
{
|
|
/// <summary>
|
|
/// Defines the conversion from a nullable <see cref="TimeOnly"/> object to a nullable <see cref="TimeSpan"/> 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 NullableTimeOnlyConverter : ValueConverter<TimeOnly?, TimeSpan?>
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="NullableTimeOnlyConverter"/> class.
|
|
/// </summary>
|
|
public NullableTimeOnlyConverter()
|
|
: base(
|
|
d => d == null ? null : new TimeSpan?(d.Value.ToTimeSpan()),
|
|
d => d == null ? null : new TimeOnly?(TimeOnly.FromTimeSpan(d.Value))
|
|
)
|
|
{ }
|
|
}
|
|
}
|
|
|
|
#endif
|