I need help for mapping enums with Dapper and Oracle.
I have a field in Oracle type NUMBER (1) and must turn into an enum in my entity.
public Status Status { get; set; }
Status is a enum:
public enum Status
{
[Description("Inactive", "0")]
Inactive = 0,
[Description("Active", "1")]
Active = 1,
}
How to make the mapping using Dapper?
Enums should JustWorkâ„¢ for either integer or string representations. If it isn't working, you'll have to be more specific about any exception that it is throwing. For example, thinking aloud and pure speculation, but: IIRC Oracle has a habit of wanting to great numbers as 64-bit, and I wonder if the ebun mapping code handles all flavors of numeric conversion. If it doesn't, then that's a bug.
So: does it work?
Just to be explicit with the sound advice in the other answers. Your database might have the data stored in a column called StatusId
whereas you object's property might simply be Status
.
I typically get around this by using an alias in the SQL query:
SELECT StatusId AS Status FROM Table
Dapper implicitly understands how to map an int
database field to a C# enum. I usually give my enums explicit values to make things as clear as possible, e.g.
public enum Status
{
Active = 1,
Inactive = 2
}