Create enum in Entity Framework 4.0

I wanted to use enum in on the my entities which I created in visual studio 2010.
But figured out that Entity Framework does not support Enum directly.
After searching for some time I found one simple workaround to use enum in entity framework 4.0
So lets see simple example of how can we create enum in entity framework 4.0

Open Entities.Designer.cs, there you can find that classes are partial

  [EdmEntityTypeAttribute(NamespaceName="MyModel", Name="IFile")]
        [Serializable()]
        [DataContractAttribute(IsReference=true)]
        public partial class IFile: EntityObject

First create an enum.

        public enum FileStatus
        {         red= 0,         blue = 1,         orange = 2
        }

In the namespace of the Entities, create a partial class as the entity you want to expand.

        public partial class IFile
        {    
             [DataMemberAttribute()]         
              public FileStatus FileStatus         
              {             get {return (FileStatus)this.Status;}
                            set{this.Status = (int)value;}
              }     
        }

Create a full property that will uses the existing property of the entity but wraps it with the enum we created as in the above example.

So now , the new property Filestatus will be available in entity for any kind of statements we use.

    context.AddToIFiles(new IFile{FileName = FileName,FileStatus = FileStatus.InProgress});

However, this property wouldn’t be available/supported any linq statements. (Or perhaps I haven’t figured that out).

    var fileEntry = (from entry in context.IFiles
        where entry.FileName == FileName &&
            //Following statment is not supported by the linq to entity, 
            //since and Edm property has to be marked as EmdPropertyAttribute. 
            //But FileStatus is not marked with an attribute.
            //entry.FileStatus == FileStatus.InProgress && 
     
            //This would work well.
            entry.Status == (int)FileStatus.InProgress
        select entry).First();

So like this you can create enum in entity framework 4.0

admin