I have a Data First Migration and have been struggling a little with storing an enum value as a string.
(Im aware that enums should be stored as ints but, personally I have never liked this, yes enums are hard and fast values but I have always percived them to vary a little bit as the code evolves, i am therefore worried about the integrity of using an int value as new values are added.)
However in my example I need to store the value as a string anyway so that other systems will be able to understand it.
So the database has the following generated property
public string billing_location { get; set; }
The View Model
public string billing_location { get; set; }
public MobiusBillingLocation BillingLocation
{
get
{
MobiusBillingLocation mbl;
if (Enum.TryParse(billing_location, true, out mbl))
{
return (MobiusBillingLocation) Enum.Parse(typeof(MobiusBillingLocation), billing_location, true);
}
return MobiusBillingLocation.None;
}
//This also needs setting in the controller code
set { billing_location = value == MobiusBillingLocation.None ? null : value.ToString(); }
}
public enum MobiusBillingLocation { None, UK, US, SGSingapore }
The code has been designed so that it will be happy with null values and translates None as null and back again.
My questions are, does this approach seem reasonable? Any ideas / suggestions for improvments? Would you use this approach for enums on a code first model for example?
UPDATE
I will add that, in this database, due to it being legacy and unnormalized (thats another topic) and has systems on it that i can't touch I cant be adding extra lookup tables at this time, the data in it however needs to have meaning to a user, ie integer values won't work as they don't pass meaning.
I am however also interested in some side discussion about use of enums.