Sheldon Warkentin Sheldon Warkentin - 1 year ago 160
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download