I have an enum defined as -
public enum MarkUpCalculationType
{
Percentage,
Fixed
}
These values are stored in the database as single values ("P" for percentage and "F" for fixed). The data is pulled from the database using a stored procedure similar to the below -
SELECT
M.MarkUpType AS MarkUpCalculationType,
-- Other selected values
FROM
dbo.MarkUp M
-- some joins
In the original version of my code it would take the returned character and set the property to the matching enum value.
I have now altered the code to use Dapper but it gives me an error when it tries to parse the enum value -
Error parsing column 15 (MarkUpCalculationType=P - String)
I've tried using a custom type handler but looking at this github issue it looks like it is not going to be fixed in the main Dapper project (and frankly I agree with Marc Gravell's reservations to fixing it)
If you are able to slightly alter the enum and stored procedure you can do the following.
Alter the enum to add the single letter code to each enumeration -
public enum MarkUpCalculationType
{
Percentage = 'P',
Fixed = 'F'
}
Now, as enums cannot be typed as chars the "P" and "F" are actually represented by their ASCII codes (80 and 70 respectively). You could write it as such also -
public enum MarkUpCalculationType
{
Percentage = 80,
Fixed = 70
}
Though using the actual character values makes the code easier to read and the link between the enum values and database values clearer.
Now if you alter your stored procedure to return the character's ASCII code Dapper should be able to to parse your enum correctly -
SELECT
ASCII(MV.MarkUpType) AS MarkUpCalculationType,
-- Other selected values
FROM
dbo.MarkUp M
-- some joins