From bacfb6374182e2f7b7983ffe33b864e84856f2c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Tue, 13 Jun 2023 11:42:09 +0200 Subject: [PATCH] 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 #295 --- Dapper/SqlMapper.cs | 2 +- tests/Dapper.Tests/Providers/MySQLTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dapper/SqlMapper.cs b/Dapper/SqlMapper.cs index f61f89c8b..01b195a2a 100644 --- a/Dapper/SqlMapper.cs +++ b/Dapper/SqlMapper.cs @@ -3716,7 +3716,7 @@ private static void LoadReaderValueOrBranchToDBNullLabel(ILGenerator il, int ind { TypeCode dataTypeCode = Type.GetTypeCode(colType), unboxTypeCode = Type.GetTypeCode(unboxType); bool hasTypeHandler; - if ((hasTypeHandler = typeHandlers.ContainsKey(unboxType)) || colType == unboxType || dataTypeCode == unboxTypeCode || dataTypeCode == Type.GetTypeCode(nullUnderlyingType)) + if ((hasTypeHandler = typeHandlers.ContainsKey(unboxType)) || colType == unboxType || dataTypeCode == (nullUnderlyingType != null ? Type.GetTypeCode(nullUnderlyingType) : unboxTypeCode)) { if (hasTypeHandler) { diff --git a/tests/Dapper.Tests/Providers/MySQLTests.cs b/tests/Dapper.Tests/Providers/MySQLTests.cs index 4b17ff80b..19b6267f5 100644 --- a/tests/Dapper.Tests/Providers/MySQLTests.cs +++ b/tests/Dapper.Tests/Providers/MySQLTests.cs @@ -96,7 +96,7 @@ public void Issue295_NullableDateTime_MySql_ConvertZeroDatetime() } } - [FactMySql(Skip = "See https://github.com/DapperLib/Dapper/issues/295, AllowZeroDateTime=True is not supported")] + [FactMySql] public void Issue295_NullableDateTime_MySql_AllowZeroDatetime() { using (var conn = Provider.GetMySqlConnection(true, false, true)) @@ -105,7 +105,7 @@ public void Issue295_NullableDateTime_MySql_AllowZeroDatetime() } } - [FactMySql(Skip = "See https://github.com/DapperLib/Dapper/issues/295, AllowZeroDateTime=True is not supported")] + [FactMySql] public void Issue295_NullableDateTime_MySql_ConvertAllowZeroDatetime() { using (var conn = Provider.GetMySqlConnection(true, true, true))