Click here to Skip to main content
Click here to Skip to main content

How to Use enum with Entity Framework 5 ?

, 26 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This blog post shows how to use enum with Entity Framework 5.

What is enum?

  • The enum keyword is used to declare an enumeration
  • A distinct type consisting of a set of named constants called the enumerator list
  • Every enumeration type has an underlying type
  • The default underlying type of the enumeration elements is int
  • By default, the first enumerator has the value 0
  • And the value of each successive enumerator is increased by 1
  • EF 5 enum can have the Types as Byte, Int16, Int32, Int64 or SByte 
  • You can download the Source code Here from the Original Article 

Let's take a simple example  

E.g. 1 :

enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth

E.g. 2 : Enumerators can have initializers to override the default values 

enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, the sequence of elements is forced to start from 1 instead of 0

How to Use enum with Entity Framework 5 Code First ?

  • This feature is Latest improvement of EF 5

Step 1 : Open Visual Studio 2012

Step 2 : File ==> New Project as EF5EnumSupport

New Project as EF5EnumSupport

Step 3 : Right Click Your Solution and then Click Manage NuGet Packages...

Manage NuGet Packages

Step 4 : Click Online Tab and Then Type Entity Framework 5 on Search Box

After that Select EntityFramework and Click Install

Select EntityFramework

Step 5 : Click I Accept

Click I Accept

After the above Step, below mentioned DLLs (Red Color) have added to the Project

DLLs (Red Color)  have added to the Project

Step 6 : Add ==> New Class and make it as enum Type Course

Solution Explorer 

Complete project


namespace EF5EnumSupport.Models
    public enum Course
        Mcsd = 1,


Step 7 : Add ==> New Class as Student


 namespace EF5EnumSupport.Models {
    public class Student
        public int Id { get; set; }
        public string Name { get; set; }
        public Course Course { get; set; }

Step 8 : Create DbContext derived Class as StudentContext


 using System.Data.Entity;
 namespace EF5EnumSupport.Models
    public class StudentContext : DbContext
        public DbSet<Student> Students { get; set; }


Step 9 : Console application's Main Method is as below


using System;
using System.Linq;
using EF5EnumSupport.Models;
namespace EF5EnumSupport
  public class Program
     static void Main(string[] args)
       using (var context = new StudentContext())
        context.Students.Add(new Student { Name = "Sampath Lokuge",
        Course = Course.Mcsd });
         var studentQuery = (from d in context.Students
                             where d.Course == Course.Mcsd
                             select d).FirstOrDefault();
         if (studentQuery != null)
           Console.WriteLine("Student Id: {0} Name: {1} Course : {2}",
               studentQuery.Id, studentQuery.Name,
               studentQuery.Course);           }


The brand new enum support allows you to have enum properties in your entity classes

enum properties in your entity classes


Step 10 : Run the Console Application

DEBUG ==> Start Without Debugging (Ctrl + F5)

Run the Console Application


Step 11 : Where's the Database ?

  • By default the database will be created on the LocalDB instance
  • The Entity Framework names the database after the fully qualified name of the derived context
  • i.e. EF5EnumSupport.Models.StudentContext

    SQL Server Object Explorer in Visual Studio 2012 is as below

    SQL Server Object Explorer in Visual Studio 2012


View Data on Students Table

View Data on Students Table


What is LocalDb ?

  • Microsoft SQL Server 2012 Express LocalDB is an execution mode of SQL Server Express targeted to program developers
  • LocalDB installation copies a minimal set of files necessary to start the SQL Server Database Engine

That's It.You're Done.


  • Entity Framework 5 brings a number of improvements and enum Support in Code First is one of them
  • You can see that when you use enum type with your model class, Visual Studio 2012 gives intellisense support out of the box
  • It's really awesome new feature of the Entity Framework 5
  • So Enjoy it

I hope this helps to you. Comments and feedback greatly appreciated. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sampath Lokuge
Software Developer (Senior) Freelancer
Sri Lanka Sri Lanka
Sampath Lokuge holds a Bachelor of Science degree in the Mathematics and Computer Science at the University of Colombo, Sri Lanka.
He possesses over 8 years of experience in constructing web applications using Microsoft Technologies including MVC, C#, SQL Server, Web API, Entity Framework and also other web technologies such as HTML5, CSS3,jQuery and AngularJS.
Sampath has earned Microsoft certifications such as MCP, MCAD, MCSD and MCTS and very recently he has completed MS (Microsoft Specialist) for MVC 4 and MCSD (Windows Store Apps Using HTML5).
Besides that, he is an active blogger, writing about web and mobile development issues and promoting best practices.He also actively participates in online communities such as Code Project and StackOverflow.He himself is handling three communities, which are MVC 5 With C# on Linkedin,Entity Framework 6 on G+ and Hybrid Mobile App with WinJS on Facebook.
Now, I am a 100% Freelancer. Smile | :)
My Email :

My Tech Blogs

Sampath Lokuge Tech Blog
Adventures in MEAN Stack World

Communities which I'm Handling :

Entity Framework 6 MVC 5 With C#
Hybrid Mobile App with WinJS
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 26 Nov 2013
Article Copyright 2013 by Sampath Lokuge
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid