EnumDataTypeAttribute 是否应该在使用实体框架的 .NET 4.0 中正常工作? [英] Should the EnumDataTypeAttribute work correctly in .NET 4.0 using Entity Framework?

查看:17
本文介绍了EnumDataTypeAttribute 是否应该在使用实体框架的 .NET 4.0 中正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举,我想将它作为某种值持久保存到底层数据库中,以便我可以来回调用它.

I have an enumeration which I'd like to persist as a value of some sort into the underlying database so that I can bring it back and forth.

我读过一些文章,建议创建一个枚举包装器,其中定义了静态隐式运算符,使用 ComplexType 对象映射进行映射,如下面的链接所述.

I have read some articles that suggest to create a enumeration wrapper with static implicit operators defined, mapped using a ComplexType object mapping as described in the link below.

如何在 EF4 中伪造枚举

此解决方案完美无缺!感谢 Alex James.

This solution works flawlessly! My thanks to Alex James.

除此之外,我发现了 EnumDataTypeAttribute类 的目的似乎是通过实体框架处理枚举持久性.我试过了,它似乎根本不起作用.这是一个代码示例.

Aside, I discovered of the EnumDataTypeAttribute Class which purpose seems to handle enums persistence through Entity Framework. I tried it and it doesn't seem to work at all. Here's a code sample.

public enum StreetDirection {
    East
    , None
    , North
    , NorthEast
    , NorthWest
    , South
    , SouthEast
    , SouthWest
    , West
}

public enum StreetType {
    Avenue
    , Boulevard
    , Court
    , Crescent
    , Drive
    , Hill
    , None
    , Road
    , Street
}

public class StreetTypeWrapper {
    public int Value {
        get {
            return (int)t;
        } 
        set {
            t = (StreetType)value;
        }
    }
    public StreetType EnumValue {
        get {
            return t;
        } 
        set {
            t = value;
        }
    }

    public static implicit operator int(StreetTypeWrapper w) {
        return w.Value;
    }

    public static implicit operator StreetType(StreetTypeWrapper w) {
        return w == null ? StreetType.None : w.EnumValue;
    }

    public static implicit operator StreetTypeWrapper(int i) {
        return new StreetTypeWrapper() { Value = i };
    }

    public static implicit operator StreetTypeWrapper(StreetType t) {
        return new StreetTypeWrapper() { EnumValue = t };
    }

    private StreetType t;
}

public class Street {
    [EnumDataType(typeof(StreetDirection))]
    public StreetDirection Direction { get; set; }
    public string Name { get; set; }
    public int StreetId { get; set; }
    public StreetTypeWrapper Type { get; set; }
}

public class StreetTypeMapping 
    : ComplexTypeConfiguration<StreetTypeWrapper> {
    public StreetTypeMapping() {
        Property(o => o.Value)
            .HasColumnName("StreetType");
    }
}

现在,如果我相信和/或正确理解 MSDN 关于 EnumDataTypeAttribute 类的说法,则 Direction 属性应该被持久化到数据库中.好吧,它没有!我找不到原因,除了 EF 不支持枚举持久性.至于 StreetTypeWrapper 及其 StreetTypeMapping 类,它确实可以完美运行.

Now, if I believe and/or understanding correctly what MSDN says about the EnumDataTypeAttribute class, the Direction property should get persisted into the database. Well, it doesn't! I can't find a reason for this, except that EF doesn't support enums persistence. As for the StreetTypeWrapper and its StreetTypeMapping class, it does work flawlessly.

是否有任何线索说明 EnumDataType 不能按预期工作?

推荐答案

这是因为.NET框架的设计缺陷..NET 框架包含著名的 System.ComponentModel.DataAnnotations 命名空间,其中定义了多个不同的属性..NET 框架的许多不同部分都在使用这个命名空间,但他们使用它来完成不同的任务,并且每个这样的部分只使用一些属性子集.这会引起很多混乱.

This is because of design flaw in .NET framework. .NET framework contains famous System.ComponentModel.DataAnnotations namespace where multiple different attributes are defined. Many different parts of .NET framework are using this namespace but they are using it to achieve different tasks and every such part use only some subset of attributes. This causes a lot of confusion.

EnumDataTypeAttribute 就是这样的例子.此属性仅适用于 ASP.NET 动态数据.它允许您使用此属性标记 int 属性,并且自动生成的 UI 将显示带有枚举值的下拉列表,而不是数值的文本框.所以存在从 enum 到 int 的映射,但它在 UI 层而不是在模型/持久层中.

EnumDataTypeAttribute is such example. This attribute is only for ASP.NET Dynamic Data. It allows you to mark int property with this attribute and automatically generated UI will show drop down with enum values instead of textbox for numeric values. So there is mapping from enum to int but it is in UI layer not in model / persistence layer.

这篇关于EnumDataTypeAttribute 是否应该在使用实体框架的 .NET 4.0 中正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆