In addition to the existing answers, there is another special case.
I found that Dapper maps enums automatically if the enum maps to a property inside a model and your Query<Model>
is of that model type.
However, when you Query<MyEnum>
directly, it throws an exception and expects an int32 from the database.
To get around this I just made a little EnumWrapper like so:
public class EnumWrapper<T> where T:struct
{
public T Value { get; set; }
}
And then proceed to query like so:
var sql = @"SELECT E.[Status] AS [Value] FROM [Exmaple].Example E";
var connection = await this.GetConnection();
var status = await connection.QuerySingleAsync<EnumWrapper<TheEnum>>(sql);
return status.Value;
It's important that the SQL alias matches the property in the EnumWrapper. In this case it's Value
.