Providing Default value for enum with OrmLite and MySql

I’d like to set a default value for an enum on a poco property to be persisted as string.

Is this supported?

I have this enum:

public enum Language
{
    En,
    Fr
}

And this property on a poco:

[Required]
public Language Language { get; set; }

How can I set the default on the above property to Language.En?

I’ve tried:

[Default(typeof(string), "En")]


[Default("En")]

Thanks!

Hi @Math,

If you are creating the table with OrmLite, the column for Language by default should be a string type (eg VARCHAR 255). And since En is your first member, it will be the default when not specified without any additional attributes. For example,

public class MyTable
{
    [AutoIncrement]
    public int Id { get; set; }
    
    [Required]
    public Language Language { get; set; }
}
db.CreateTableIfNotExists<MyTable>();
db.Insert(new MyTable());
// Inserted will be 1, "En"

Visuals from DataGrip:

image
image

Are you seeing something else? If so, let us know what versions of ServiceStack and MySql you are using.

Works perfectly.

Seems I was trying to complicate things for nothing…