Is It Possible to Map to an Enum With an Alias?

Viewed 423

I have a http request coming that has a property for a value. There are 2 possible options for this value, let's say Standard and Boosted. I'm using an enum for this.

I also need to get the same value from my database, but in database this value is called something else. For example they are called TypeS and TypeB.

I'm looking for an easy way to map them to each other.

I've tried using an Alias but that doesn't seem to be working.

pubic enum RequestType
{
    [Alias("TypeS")]
    Standard,
    [Alias("TypeB")]
    Boosted
}

public class ReturnObject
{
    public RequestType type {get; set;}
}

I'm getting the record from the database using a stored proc.

db.SqlList<ReturnObject>("EXEC SomeStoredProc @someParameter",
                    new { someParameter }).ToList();

ReturnObject.type is always Standard even tho the Database returns TypeB which tells me it instantiates the enum with the default value.

1 Answers

In the latest v5.5.1 on MyGet, OrmLite supports char enums, e.g

[EnumAsChar]
public enum SomeEnum
{
    Value1 = 'A', 
    Value2 = 'B', 
    Value3 = 'C', 
    Value4 = 'D'
}

Otherwise the only other ways OrmLite supports persisting enums is by Name (default):

public enum SomeEnum
{
    Value1, 
    Value2, 
    Value3, 
    Value4,
}

By Enum integer value:

[Flags] //or [EnumAsInt]
public enum SomeEnum
{
    None = 0,
    Value1 = 1 << 0, 
    Value2 = 1 << 1, 
    Value3 = 1 << 2, 
    Value4 = 1 << 3,
}

Or by char value as shown in my first example.

Alternatively you'd need to use the name it's stored in the database as, but you can have it serialized using a different value by annotating it with [EnumMember], e.g:

[DataContract]
public enum SomeEnum
{
    [EnumMember(Value = "VALUE 1")]
    Value1, 
    [EnumMember(Value = "VALUE 2")]
    Value2, 
    [EnumMember(Value = "VALUE 3")]
    Value3, 
    [EnumMember(Value = "VALUE 4")]
    Value4,
}
Related