Skip to content

Commit

Permalink
Fix InvalidCastException when reading MySql DateTime
Browse files Browse the repository at this point in the history
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
  • Loading branch information
0xced committed Jun 13, 2023
1 parent a9ef55f commit bacfb63
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Dapper.Tests/Providers/MySQLTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit bacfb63

Please sign in to comment.