Skip to content

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

Open
@AkazawaYun

Description

@AkazawaYun

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 !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions