Supporting Enum in EF 5 with .Net 4.0 I'm currently working through Getting Started with EF5 using MVC 4 in Visual Studio 2010 (.NET 4.0) to learn about how to use ASP.NET and Entity Framework. During the beginning of the tutorial I ran into an issue with enum support when creating a database with Code First Migrations. For reasons unknown I have to work in this environment and create my own support for enums so I decided to make the property int instead then I would convert it to Enum when I want to display it like so:

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq;
using System.Linq.Expressions;

namespace ContosoUniversity.Extensions
{
    public static class Extensions
    {
        public static String DisplayAsEnum&lt;TModel, TValue&gt;(this HtmlHelper&lt;TModel&gt; cHtml,
                                                           Expression&lt;Func&lt;TModel, TValue&gt;&gt; cExpression,
                                                           Type cType,
                                                           String strDisplay = "Unknown")
        {
            try
            {
                if (cType.IsEnum)
                {
                    object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

                    if (cResult != null)
                    {
                        strDisplay = Enum.ToObject(cType, cResult).ToString();
                    }
                }
            }
            catch (Exception cExc)
            {
                strDisplay = cExc.ToString();
            }

            return (strDisplay);
        }
    }
}


In my razor cshtml file I display it to the webpage with this call:

@Html.DisplayAsEnum(modelItem =&gt; item.Grade, typeof(Grade), "No Grade")


I have a feeling that this is not the "professional" or "right" way of handling the lack of enum support so I was wondering if there is anyway to improve this function or is there a better way of handling enumerations in EF 5 with .Net 4.0 support?

I was shown a Stack Overflow question that provides me with a better solution to handling the enum support. It is very helpful and I am using it at the moment, but since I do like the function that I made, is it possible to improve it?
Ideally you won't want to change the strDisplay, so we should probably take it out of the parameter list and make it a constant inside the method and then just return in each of the 3 circumstances.

var display = "Unknown";

try
{
    if (cType.IsEnum)
    {
        object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

        if (cResult != null)
        {
            return Enum.ToObject(cType, cResult).ToString();
        }
    }
}
catch (Exception cExc)
{
    return cExc.ToString();
}

return display;


this seems weird to me that we are using the variable (whatever you choose to name it)  only once, I would probably in this circumstance (since we aren't allowing it to be changed)  just return the string Unknown like this

try
{
    if (cType.IsEnum)
    {
        object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

        if (cResult != null)
        {
            return Enum.ToObject(cType, cResult).ToString();
        }
    }
}
catch (Exception cExc)
{
    return cExc.ToString();
}

return "Unknown";