It is my understanding that Enum will be stored as varchar(max).
I see that the table generated from the code first class is correctly reflected of each other.
What I do not see is the storage of the Enum property value.
The trimmed down class:
[Alias( "NonJobEvents" )]
public sealed class NonJobEvent
{
[AutoIncrement]
public int Id { get; set; }
public Enum EventType { get; set; }
}
The excluded properties of the above class get properly stored in the database. Setting JsConfig.TreatEnumAsInteger = true makes no difference in the resulting output. The EventType property column is always NULL.
The EventType property gets set to values from a third party API, each enum value is assigned an integer. e.g. var e = new NonJobEvent { EventType = TrackingEvent.Login };
I’m looking for insight as to what I may not be seeing or doing.
By default Enum’s are stored as their String name, if you don’t specify an enum value it defaults to the Enum of default(Enum) which is behaving as expected where inserting a row without specifying an Enum, e.g:
public enum SomeEnum
{
Value1,
Value2,
Value3
}
public class TypeWithEnum
{
public int Id { get; set; }
public SomeEnum EnumValue { get; set; }
}
LogManager.LogFactory = new ConsoleLogFactory();
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<TypeWithEnum>();
var obj = new TypeWithEnum { Id = 1 };
db.Insert(obj);
}
Why is this the case that the specific value type is required to define the property so that it can be stored as a string or an integer?
Take the following for example where both properties are of Enum : ValueType and are assigned value types, we’re easily able to determine both the type and the value.
You can’t use an unknown Enum Type as OrmLite needs to know the Type to dehyrdate the data into (from the raw string or integer value stored in the db), same goes for object, dynamic and other unknown types.
If you want to be able to store any enum value use a string or int property and have your App’s logic decode it into the correct enum when needed.