Skip to content

Commit fcf8b47

Browse files
committed
Fix InvalidCastException when reading MySql DateTime
Before this commit, the following exception would be thrown: > System.InvalidCastException > Unable to cast object of type 'MySqlConnector.MySqlDateTime' to type 'System.Nullable`1[System.DateTime]'. > at Deserialize74d21eca-a78e-49dc-b635-d007c613cb51(DbDataReader ) Fixes DapperLib#295
1 parent a9ef55f commit fcf8b47

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

Dapper/SqlMapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
License: http://www.apache.org/licenses/LICENSE-2.0
33
Home page: https://github.com/DapperLib/Dapper-dot-net
44
*/
@@ -3716,7 +3716,7 @@ private static void LoadReaderValueOrBranchToDBNullLabel(ILGenerator il, int ind
37163716
{
37173717
TypeCode dataTypeCode = Type.GetTypeCode(colType), unboxTypeCode = Type.GetTypeCode(unboxType);
37183718
bool hasTypeHandler;
3719-
if ((hasTypeHandler = typeHandlers.ContainsKey(unboxType)) || colType == unboxType || dataTypeCode == unboxTypeCode || dataTypeCode == Type.GetTypeCode(nullUnderlyingType))
3719+
if ((hasTypeHandler = typeHandlers.ContainsKey(unboxType)) || colType == unboxType || dataTypeCode == (nullUnderlyingType != null ? Type.GetTypeCode(nullUnderlyingType) : unboxTypeCode))
37203720
{
37213721
if (hasTypeHandler)
37223722
{

tests/Dapper.Tests/Providers/MySQLTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Data;
33
using System.Data.Common;
44
using System.Linq;
@@ -96,7 +96,7 @@ public void Issue295_NullableDateTime_MySql_ConvertZeroDatetime()
9696
}
9797
}
9898

99-
[FactMySql(Skip = "See https://github.com/DapperLib/Dapper/issues/295, AllowZeroDateTime=True is not supported")]
99+
[FactMySql]
100100
public void Issue295_NullableDateTime_MySql_AllowZeroDatetime()
101101
{
102102
using (var conn = Provider.GetMySqlConnection(true, false, true))
@@ -105,7 +105,7 @@ public void Issue295_NullableDateTime_MySql_AllowZeroDatetime()
105105
}
106106
}
107107

108-
[FactMySql(Skip = "See https://github.com/DapperLib/Dapper/issues/295, AllowZeroDateTime=True is not supported")]
108+
[FactMySql]
109109
public void Issue295_NullableDateTime_MySql_ConvertAllowZeroDatetime()
110110
{
111111
using (var conn = Provider.GetMySqlConnection(true, true, true))

0 commit comments

Comments
 (0)