DapperLib / DapperLib/Dapper

Throw InvalidCastException with " ORDER BY DESC\ASC " when SqlMapper CONVERT a char-type-db-column TO a string-type-c#-property

Open
#2,105 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

System.InvalidCastException:Error parsing column 0 (id='a-char-type-value-in-datebase' - Object)
in /_/Dapper/SqlMapper.cs:line 3928
at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value)
at Deserialize8cb9bee9-c2a8-4fec-bfdc-167531fec084(DbDataReader)

This is example:

public class user
{
public string id {get;set;} // char(36) in db
public int age {get;set;} // int in db
}
SELECT * FROM user WHERE true ORDER BY age ; =>this is OK !
SELECT * FROM user WHERE true ORDER BY age ASC ; =>this is Throw InvalidCastException !
SELECT * FROM user WHERE true ORDER BY age DESC ; =>this is Throw InvalidCastException !

Change the type of the id-column in mysql : char(36) → varchar(36) , then :

SELECT * FROM user WHERE true ORDER BY age ; =>this is OK !
SELECT * FROM user WHERE true ORDER BY age ASC ; =>this is OK !
SELECT * FROM user WHERE true ORDER BY age DESC ; =>this is OK !

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Dapper/SqlMapper.cs around line 3928 and reproduce the issue with MySQL using a char(36) id column, a string C# property, and ORDER BY age with ASC or DESC. Compare the behavior with no direction and with varchar(36); done means the explicit ORDER BY directions no longer produce InvalidCastException for the shown mapping.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, mysql
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.