diff --git a/Dapper/SqlMapper.cs b/Dapper/SqlMapper.cs index c6374075..47e40ea4 100644 --- a/Dapper/SqlMapper.cs +++ b/Dapper/SqlMapper.cs @@ -202,7 +202,7 @@ public static implicit operator TypeMapEntry(DbType dbType) static SqlMapper() { typeMap = new Dictionary(41 -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER && DATEONLY + 4 // {Date|Time}Only[?] #endif ) @@ -248,7 +248,7 @@ static SqlMapper() [typeof(SqlDecimal?)] = TypeMapEntry.DecimalFieldValue, [typeof(SqlMoney)] = TypeMapEntry.DecimalFieldValue, [typeof(SqlMoney?)] = TypeMapEntry.DecimalFieldValue, -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER && DATEONLY [typeof(DateOnly)] = TypeMapEntry.DoNotSetFieldValue, [typeof(TimeOnly)] = TypeMapEntry.DoNotSetFieldValue, [typeof(DateOnly?)] = TypeMapEntry.DoNotSetFieldValue, diff --git a/tests/Dapper.Tests/DateTimeOnlyTests.cs b/tests/Dapper.Tests/DateTimeOnlyTests.cs index 015af2d5..68e8bc17 100644 --- a/tests/Dapper.Tests/DateTimeOnlyTests.cs +++ b/tests/Dapper.Tests/DateTimeOnlyTests.cs @@ -9,7 +9,7 @@ namespace Dapper.Tests; [Collection("DateTimeOnlyTests")] public sealed class SystemSqlClientDateTimeOnlyTests : DateTimeOnlyTests { } */ -#if MSSQLCLIENT +#if MSSQLCLIENT && DATEONLY [Collection("DateTimeOnlyTests")] public sealed class MicrosoftSqlClientDateTimeOnlyTests : DateTimeOnlyTests { } #endif @@ -17,8 +17,11 @@ public abstract class DateTimeOnlyTests : TestBase where T { public class HazDateTimeOnly { + public string Name { get; set; } public DateOnly Date { get; set; } public TimeOnly Time { get; set; } + public DateOnly? NDate { get; set; } + public TimeOnly? NTime { get; set; } } [Fact] @@ -27,12 +30,18 @@ public void TypedInOut() var now = DateTime.Now; var args = new HazDateTimeOnly { + Name = nameof(TypedInOut), Date = DateOnly.FromDateTime(now), Time = TimeOnly.FromDateTime(now), + NDate = DateOnly.FromDateTime(now), + NTime = TimeOnly.FromDateTime(now), }; - var row = connection.QuerySingle("select @date as [Date], @time as [Time]", args); + var row = connection.QuerySingle("select @name as [Name], @date as [Date], @time as [Time], @ndate as [NDate], @ntime as [NTime]", args); + Assert.Equal(args.Name, row.Name); Assert.Equal(args.Date, row.Date); Assert.Equal(args.Time, row.Time); + Assert.Equal(args.NDate, row.NDate); + Assert.Equal(args.NTime, row.NTime); } [Fact] @@ -41,12 +50,18 @@ public async Task TypedInOutAsync() var now = DateTime.Now; var args = new HazDateTimeOnly { + Name = nameof(TypedInOutAsync), Date = DateOnly.FromDateTime(now), Time = TimeOnly.FromDateTime(now), + NDate = DateOnly.FromDateTime(now), + NTime = TimeOnly.FromDateTime(now), }; - var row = await connection.QuerySingleAsync("select @date as [Date], @time as [Time]", args); + var row = await connection.QuerySingleAsync("select @name as [Name], @date as [Date], @time as [Time], @ndate as [NDate], @ntime as [NTime]", args); + Assert.Equal(args.Name, row.Name); Assert.Equal(args.Date, row.Date); Assert.Equal(args.Time, row.Time); + Assert.Equal(args.NDate, row.NDate); + Assert.Equal(args.NTime, row.NTime); } [Fact] @@ -54,11 +69,14 @@ public void UntypedInOut() { var now = DateTime.Now; var args = new DynamicParameters(); + var name = nameof(UntypedInOut); var date = DateOnly.FromDateTime(now); var time = TimeOnly.FromDateTime(now); + args.Add("name", name); args.Add("date", date); args.Add("time", time); - var row = connection.QuerySingle("select @date as [Date], @time as [Time]", args); + var row = connection.QuerySingle("select @name as [Name], @date as [Date], @time as [Time]", args); + Assert.Equal(name, (string)row.Name); // untyped, observation is that these come back as DateTime and TimeSpan Assert.Equal(date, DateOnly.FromDateTime((DateTime)row.Date)); Assert.Equal(time, TimeOnly.FromTimeSpan((TimeSpan)row.Time));