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;
}
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; }
}
public void SetValue(IDbDataParameter parameter, object value) { parameter.Value = value; } }