关于CodeFrist下EntityFramework5.0及其最新版本中枚举的使用

早期使用EntityFramework的Coder应该知道5.0之前的版本是未对枚举进行支持的。不论是ModelFrist还是CodeFrist

在CodeFrist模式下一般是使用第三方解决方案,例如下面的方式虽然解决,但是在Model的声明和View的使用上都比较混乱

 Enum

    public enum PermissionTypeEnum
    {
        [Description("页面类")]
        页面类,
        [Description("操作类")]
        操作类
    }

Model

        public PermissionTypeEnum PermissionTypeEnum { get; set; }

        [Display(Name = "权限类型")]
        public int PermissionType
        {
            get { return (int)PermissionTypeEnum; }
            set { PermissionTypeEnum = (PermissionTypeEnum)value; }
        }

 EF在5.0之后就已经对枚举进行支持了,当然使用上也很简单。在官方adonet博客上有介绍

 http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-enums-june-ctp.aspx

 DBFrist的比较简单,就不说了。在这里分享一下如何在CodeFrist下的用法,希望对大家能有帮助

 Model变的更简洁

         [Display(Name = "权限类型")]
        public PermissionTypeEnum PermissionType { get; set; }

   View的Create Or Edit代码修改成如下,List不变

@Html.EnumDropDownListFor(model => model.PermissionType)

  效果如下

 

     View用到的扩展方法:

View Code
        private static Type GetNonNullableModelType(ModelMetadata modelMetadata)
        {
            Type realModelType = modelMetadata.ModelType;

            Type underlyingType = Nullable.GetUnderlyingType(realModelType);
            if (underlyingType != null)
            {
                realModelType = underlyingType;
            }
            return realModelType;
        }

        private static readonly SelectListItem[] SingleEmptyItem = new[] { new SelectListItem { Text = "请选择...", Value = "" } };

        public static string GetEnumDescription<TEnum>(TEnum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if ((attributes != null) && (attributes.Length > 0))
                return attributes[0].Description;
            else
                return value.ToString();
        }

        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
        {
            return EnumDropDownListFor(htmlHelper, expression, null);
        }

        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            Type enumType = GetNonNullableModelType(metadata);
            IEnumerable<TEnum> values = Enum.GetValues(enumType).Cast<TEnum>();

            IEnumerable<SelectListItem> items = from value in values
                                                select new SelectListItem
                                                {
                                                    Text = GetEnumDescription(value),
                                                    Value = value.ToString(),
                                                    Selected = value.Equals(metadata.Model)
                                                };

            // If the enum is nullable, add an 'empty' item to the collection
            if (metadata.IsNullableValueType)
                items = SingleEmptyItem.Concat(items);

            return htmlHelper.DropDownListFor(expression, items, htmlAttributes);
        }

 

posted @ 2013-04-27 11:44  leeolevis  阅读(1458)  评论(8编辑  收藏  举报