Map string column in Entity Framework to Enum

前端 未结 8 1301
被撕碎了的回忆
被撕碎了的回忆 2021-02-18 13:46

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.

8条回答
  •  天命终不由人
    2021-02-18 14:26

    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.

提交回复
热议问题