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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM