I have a database that has been used to store nullable ThingType?
enum as integer values:
public enum ThingType
{
Good = 0,
Bad = 1,
Annoying = 2,
}
But that has meant custom code to accommodate null values elsewhere in my project. I want to modify my enum to have a "Not Specified" value for ThingType
, as defined below:
public enum ThingType
{
NotSpecified = -1,
Good = 0,
Bad = 1,
Annoying = 2,
}
What's the best way to implement this translation in EF without making changes to the database, so that null
row values are translated to ThingType.NotSpecified
and ThingType.NotSpecified
is inserted as a null
value?
Use a second property
// Map this one to DB
public ThingType? ThingTypeDb
{
get { return ThingType == ThingType.NotSpecified ? (ThingType?)null : ThingType; }
set { ThingType = value == null ? ThingType.NotSpecified : value.Value; }
}
// Don't map this one. Use it in your logic
public ThingType ThingType { get; set; }