Sheldon Warkentin Sheldon Warkentin - 1 month ago 22
C# Question

Map string column in Entity Framework to Enum

Is there a way to map a string column to an enum in an Entity Model?

I have done this in Hibernate, but can't figure it out in EMF.

Answer

It is ugly, but for mapping enums to strings I found something like this:

public virtual string StatusString
{
    get { return Status.ToString(); }
    set { OrderStatus newValue; 
          if (Enum.TryParse(value, out newValue))
          { Status = newValue; }
        }
}

public virtual OrderStatus Status { get; set; } 

OrderStatus is the enumerator type, Status is the enumerator and StatusString is the string version of it.

Comments