.net - 应对落后的枚举支持

标签 .net entity-framework enums wcf-data-services

在 EF 5.0(计划很快发布)之前, Entity Framework 将不支持枚举。

Entity Framework 4.2 enum support

http://blogs.msdn.com/b/efdesign/archive/2011/06/29/enumeration-support-in-entity-framework.aspx

http://visualstudiomagazine.com/blogs/data-driver/2012/01/entity-framework-4-3-gets-final-tune-up.aspx

WCF 数据服务(和 oData 标准)不支持枚举

We understand that enums are common, unfortunately they never met tha bar so far. They are rather high on our list of things to do next though

(参见:https://stackoverflow.com/a/3571378/141172)

我开始了一个新项目,正在用类似的东西替换枚举:

public static class MyEnum
{
    public const int MyValue = 0;
    public const int AnotherValue = 1;
}

我牺牲了枚举提供的保证,即只有定义的值才会被分配,以便利用重要的(并且在这一点上相当成熟的)基础设施服务。

是否有更好的方法来处理滞后的枚举支持?一旦 EF 和 WCF 数据服务添加了枚举支持,是否可能会出现另一个重要的框架,它会像这两个框架一样缓慢地引入枚举支持?

最佳答案

你能否使用一个带有私有(private)构造函数的结构,为每个值定义一个公共(public)静态成员:

public struct FakeEnum
    {
        public static readonly FakeEnum MyValue = new FakeEnum(0);
        public static readonly FakeEnum AnotherValue = new FakeEnum(1);

        private readonly int _value;

        private FakeEnum(int value)
        {
            _value = value;
        }

        public int Value { get { return _value; } }

        // TODO: Equals and GetHasCode and Operators Oh My!
    }

注意:我还没有实际尝试过。

关于.net - 应对落后的枚举支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9185116/

相关文章:

c# - 复合键 Entity Framework 中的自动增量 ID

c# - Entity Framework 中的导航属性问题

java - org.hibernate.HibernateException : Error while accessing enum. 值() : class com. mksoft.fbautomate.domain.Account$Type

c++ - 以通用方式选择有效的随机枚举值

java - 用于解析 java 枚举的正则表达式

.net - 检索 ASP.NET MVC 中的当前 View 名称?

c# - 使用c#在MSChart中显示系列的具体方法

.net - WPF 如何决定对缺失字符使用哪种字体?

c# - 解密 1 个字节后无法打开 xml?

.net - Entity Framework 迁移: PrimaryKey