【问题标题】:Enumeration Objects (Strings) in Entity Framework实体框架中的枚举对象(字符串)
【发布时间】:2016-02-28 11:13:33
【问题描述】:

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

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

我需要一种方法将图标名称保持为固定枚举,以便从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 中轻松进行智能感知吗?

【问题讨论】:

    标签: c# entity-framework enums icons constants


    【解决方案1】:

    你可以:

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

    【讨论】:

      【解决方案2】:

      您是否考虑过使用字符串常量?

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

      【讨论】:

        【解决方案3】:

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

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

        剃刀视图:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多