Search code examples
c#stringenumslinq2db

LinqToDB how to store enum as string value?


Can LinqToDB store the value of an Enum property as the string value of the Enum instead of the integer value?

public enum OrderType
{
    New,
    Cancel
}

[Table("Orders")]
public class Order
{
    [PrimaryKey, Identity]
    public int OrderId { get; set; }

    [Column]
    public OrderType Type { get; set; }
}

How can I get LinqToDB to store "New" or "Cancel" to the database instead of 0 or 1?

Update: Seems the answer for LinqToDB is to set the MapValue attribute on the enum. I had been trying to find where to specify it on the data object.

public enum OrderType
{
    [LinqToDB.Mapping.MapValue(Value = "NEW")]
    New,
    [LinqToDB.Mapping.MapValue(Value = "CANCEL")]
    Cancel
}

This will store the specified value on the enum as text in the database.


Solution

  • Seems the answer for LinqToDB is to set the MapValue attribute on the enum. I had been trying to find where to specify it on the data object.

    public enum OrderType
    {
        [LinqToDB.Mapping.MapValue(Value = "NEW")]
        New,
        [LinqToDB.Mapping.MapValue(Value = "CANCEL")]
        Cancel
    }