Invalid cast exception. Workaround to convert data type.
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
We've started using Dapper with our Ingres database, of which we have tinyint datatypes. The Ingres .net driver maps this to sbyte data type. This was causing us invalid cast exception but seemed to get around it by writing a basic SByteTypeHandler as shown below.
For other reasons we don't want to use sbyte in our POCOs when accessing our Ingres data via Dapper.
Is there a way in code I can tell Dapper to use a specific data type?
Here's some pseudo code/table script related to my issue:
create table Person
{
PersonName char(64) not null not default,
Age tinyint not null not default // this is our Ingres tinyint column data type
}
class PersonPOCO
{
public string PersonName { get; set; }
public sbyte Age { get; set; }
}
This type handler below seems to help us resolve the 'invalid cast exception' we were getting with tinyint to sbyte.
***** What we'd like to have is the 'Age' field in PersonPOCO to be an Int32, but haven't been able to figure out how.**
internal class SByteTypeHandler : Dapper.SqlMapper.ITypeHandler
{
public object Parse(Type destinationType, object value)
{
switch(value)
{
case int intValue:
return intValue;
}
return Convert.ToSByte(value);
}
public void SetValue(IDbDataParameter parameter, object value)
{
parameter.Value = value;
}
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue provides no repository file or test to start from. Begin with the supplied SByteTypeHandler and investigate Dapper's type-handler mapping for database values; done means an Int32 Age property can read the Ingres tinyint value without an invalid cast exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100