Skip to content

Custom type handlers for enums are ignored #259

Description

@chilversc

I have an enum that is serialized to the DB using a single letter code.
I tried adding a custom type handler for the enum to handle the conversion but dapper seems to ignore the custom type handler.

Given the example code I expected to see the values;
ID = 10, Type = B
ID = 1, Type = Foo

What I actually got was:
ID = 1, Type = 2
DataException, Error parsing column 1 (Type=F - String)


voidMain(){SqlMapper.AddTypeHandler(newItemTypeHandler());using(varconnection=newSqlConnection("Server=(local); Database=tempdb; Integrated Security=SSPI")){connection.Open();connection.Query("SELECT @ID AS ID, @Type AS Type",newItem{ID=10,Type=ItemType.Bar}).Dump();connection.Query<Item>("SELECT 1 AS ID, 'F' AS Type").Dump();}}classItemTypeHandler:SqlMapper.TypeHandler<ItemType>{publicoverrideItemTypeParse(objectvalue){varc=((string)value)[0];switch(c){case'F':returnItemType.Foo;case'B':returnItemType.Bar;default:thrownewArgumentOutOfRangeException();}}publicoverridevoidSetValue(IDbDataParameterp,ItemTypevalue){p.DbType=DbType.AnsiStringFixedLength;p.Size=1;if(value==ItemType.Foo)p.Value="F";elseif(value==ItemType.Bar)p.Value="B";elsethrownewArgumentOutOfRangeException();}}classItem{publiclongID{get;set;}publicItemTypeType{get;set;}}enumItemType{Foo=1,Bar=2,}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv3.0Changes awaiting the next breaking release

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions