Text column cannot be mapped to nullable enum
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
I have a table with a text column that allows nulls. This text column holds the string representation of an enum:
public enum Status
{
NotConnected,
Connected
}
When making updates or inserts into this table, it works just fine because I can use the ?. null-conditional operator which seems to make Dapper happy. However, when doing reads I get an error:
System.Data.DataException: Error parsing column 3 (status= - String)
---> System.ArgumentException: Must specify valid information for parsing in the string. (Parameter 'value')
at System.Enum.TryParse(Type enumType, String value, Boolean ignoreCase, Boolean throwOnFailure, Object& result)
at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
at Deserialize2098d38f-668f-47dd-8226-fb67d62b44fc(IDataReader )
--- End of inner exception stack trace ---
at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3665
at Deserialize2098d38f-668f-47dd-8226-fb67d62b44fc(IDataReader )
at Dapper.SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command)
presumably because dapper does not know what to do if the column is null. Is there a way to get Dapper to do the right thing, maybe through custom type handlers or mappers?
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
Start at SqlMapper.cs line 3665 and the enum parsing path shown in the stack trace; compare how nullable database values are handled during reads with the custom type handlers or mappers suggested in the issue. Reproduce the nullable text-column read and verify that a null value maps without the DataException while non-null enum strings still work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100