如果EnumDataTypeAttribute在.NET 4.0中正常工作,使用实体框架? [英] Should the EnumDataTypeAttribute work correctly in .NET 4.0 using Entity Framework?

查看:709
本文介绍了如果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枚举

该解决方案完美的作品!我要感谢亚历克斯詹姆斯

This solution works flawlessly! My thanks to Alex James.

除此之外,我发现的<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.enumdatatypeattribute.aspx\"相对=nofollow> EnumDataTypeAttribute类 其目的似乎是处理枚举通过持久性实体框架。我试了一下,它似乎并没有在所有的工作。这里有一个code样本。

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 类,在方向属性应该得到持续到数据库中。那么,它不是!我无法找到一个原因,除了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属性,并自动生成用户界面将显示下拉与枚举值,而不是为文本框数值。所以从映射枚举为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天全站免登陆