Should the EnumDataTypeAttribute work correctly in .NET 4.0 using Entity Framework?

时光怂恿深爱的人放手 提交于 2019-12-30 07:24:22

问题


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.

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.

How to fake Enums in EF4

This solution works flawlessly! My thanks to Alex James.

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");
    }
}

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.

Are there any clue why the EnumDataType shouldn't work as expected?


回答1:


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 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.



来源:https://stackoverflow.com/questions/8076940/should-the-enumdatatypeattribute-work-correctly-in-net-4-0-using-entity-framewo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!