Using enums in WCF Data Services

前端 未结 6 2026
独厮守ぢ
独厮守ぢ 2020-12-14 09:43

I\'m trying to manually build a WCF Data Service using a POCO data model and I cannot figure out how to properly expose enum values. Assuming a simple model lik

6条回答
  •  别那么骄傲
    2020-12-14 10:23

    As follow-up, the "wrapper" approach is ultimately what worked. Essentially, a small class is written to wrap the enum and return primitive int values in the Data Service:

    [IgnoreProperties("EnumValue")]
    public class OrderStatusWrapper
    {
        private OrderStatus _t;
    
        public int Value
        {
            get{ return (int)_t; }
            set { _t = (OrderStatus)value; }
        }
    
        public OrderStatus EnumValue
        {
            get { return _t; }
            set { _t = value; }
        }
    
        public static implicit operator OrderStatusWrapper(OrderStatus r)
        {
            return new OrderStatusWrapper { EnumValue = r };
        }
    
        public static implicit operator OrderStatus(OrderStatusWrapper rw)
        {
            if (rw == null)
                return OrderStatus.Unresolved;
            else
                return rw.EnumValue;
        }
    }  
    

    This was largely based on the advice given for working around EF4's enum limits:

    http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx

    Hope this technique helps others that follow.

提交回复
热议问题