实体框架中的枚举对象(字符串)

时间:2016-02-28 11:13:33

标签: c# entity-framework enums icons constants

我正在使用Entity Framework构建模型并购买了响应式CSS。

内置固定图标附带CSS。如下(Name and Icon Class Value)

enter image description here

我需要一种方法将图标名称保持为固定枚举,以便从 VS intellisense 访问它。目前,我们不能将实体表存储在实体框架中(因为它需要与难以维护的表的关系),并且枚举不允许字符串类型。

无效的代码:

    public sealed class IconType
    {
        public static readonly IconType Rupee_Icon = new IconType("rupee-icons");
        public static readonly IconType Doller_Icon = new IconType("doller-icon");

        private IconType(int EnumID,string EnumObjectValue)
        {            
            IconValue = EnumObjectValue;
        }

        public string IconValue { get; private set; }
    }

更多无效的代码(CSS类名称包含ui bell icon等空格:

public enum Icon
{
        NotSet=0,
        Idea  Icon=1,
        Bell Icon =2
}

有没有其他方法可以在EF中使用名称/对象作为枚举或常量,以便在Visual Studio中轻松实现智能感知?

3 个答案:

答案 0 :(得分:0)

你可以:

  1. 省略枚举中的空格:

    public enum Icon
    {
        NotSet = 0,
        IdeaIcon = 1,
        BellIcon = 2
    }
    
  2. 在枚举中添加说明或名称(甚至是某些自定义属性)属性:

    public enum Icon
    {
        NotSet = 0,
    
        [Description("ui idea icon")]
        IdeaIcon = 1,
    
        [Description("ui bell icon")]
        BellIcon = 2
    }
    
  3. 需要时获取说明名称。获取描述属性值的示例方法:

        public static string GetDescription<T>(this T enumerationValue)
        where T : struct, IConvertible
    {
        var type = enumerationValue.GetType();
        if (!type.IsEnum)
        {
            throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
        }
    
        // Tries to find a DescriptionAttribute for a potential friendly name for the enum
        var memberInfo = type.GetMember(enumerationValue.ToString(CultureInfo.InvariantCulture));
        if (memberInfo.Length > 0)
        {
            var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                // Pull out the description value
                return ((DescriptionAttribute)attributes[0]).Description;
            }
        }
    
        // If we have no description attribute, just return the ToString of the enum
        return enumerationValue.ToString(CultureInfo.InvariantCulture);
    }
    

答案 1 :(得分:0)

你考虑过使用字符串常量吗?

public static class IconType
{
    public const string RUPEE_ICON = "rupee-icon";
    public const string DOLLER_ICON = "doller-icon";
    // ...
}

答案 2 :(得分:0)

将图标存储为普通旧对象。为什么要使用实体框架呢?

public static class Icons
{
    public enum Type
    {
        IdeaIcon = 1,
        BellIcon =2
    }

    public static Icon Get(Type type)
    {
        return IconCollection.Single(icon => icon.Type == type);
    } 

    static IEnumerable<Icon> IconCollection
    {
        get
        {
            return new List<Icon>
            {
                new Icon(Type.IdeaIcon, "Idea Icon", "icon idea-icon"),
                new Icon(Type.BellIcon, "Bell Icon", "icon bell-icon"),
            };
        }
    }

    public class Icon
    {
        public Icon(Type type, string description, string cssClass)
        {
            Type = type;
            Description = description;
            CssClass = cssClass;
        }
        public Type Type { get; private set; }
        public string Description { get; private set; }
        public string CssClass { get; private set; }
    }
}

在代码中使用:

public class Class1
{
    public void Method1()
    {
        var ideaIcon = Icons.Get(Icons.Type.IdeaIcon);
        var x = ideaIcon.CssClass;
        var y = ideaIcon.Description;

        var bellIcon = Icons.Get(Icons.Type.BellIcon);
        // etc...
    }
}

Razor观点:

@Icons.Get(Icons.Type.BellIcon).CssClass

如果您需要枚举图标集,则可以轻松地向Icons类添加另一个静态访问器。