Enum Support (Code First) in Entity Framework 5

Entity Framework 5 brings a number of improvements and Enum Support in Code First is one of them.
 
In this article, I'll follow the simple steps to develop a console app with Entity Framework Code First.
 
Step 1: Create a New Project
 
Create a new console application File > New > Project > Visual C# > Console Application.
 
Step 2: EF5 from NuGet
 
At the very first, we need to enable the Entity Framework 5 for this console project from the NuGet Package Manager. For this, in "Solution Explorer" right-click on the project and click on "Manage NuGet Packages" and install Entity Framework 5.
 
 
The DbContext and DbSet types are defined in the EntityFramework assembly; that's why I added a reference to this DLL using the EntityFramework NuGet package. Note, that in addition to the EntityFramework assembly, references to System.ComponentModel.DataAnnotations and System.Data.Entity assemblies are automatically added to the project as well.
 
Step 3: Create a Model
 
Now, time to define a new model using the Code First Approach.
  1. public enum Courses  
  2. {  
  3.     BCA, MCA, BTECH, MTECH  
  4. }  
  5.   
  6. public partial class Students  
  7. {  
  8.     public int ID { getset; }  
  9.     public string Name { getset; }  
  10.     public Courses Course { getset; }  

Please note, the preceding code defines an "enum" by the name "Courses". By default, the enumeration is of int type. The "Course" property on the "Students" class is of the "Courses" enumeration type. Remember, in EF5 enumeration can have the following underlying types:  Byte, Int16, Int32, Int64, or SByte.
 
Step 4: Create DbContext
 
Now, go ahead and define the DbContext.
  1. public partial class StudentContext : DbContext  
  2. {  
  3.     public DbSet<Students> Student { getset; }  

Note to use a namespace "using System.Data.Entity;" on the top.
 
Step 5: Data Handling
 
Now, we are all set to retrieve data and for this, we need some code in the Main() method; see:
  1. static void Main(string[] args)  
  2. {  
  3.     using (var context = new StudentContext())  
  4.     {  
  5.         context.Student.Add(new Students { Name = "Abhimanyu", Course = Courses.MTECH });  
  6.         context.SaveChanges();  
  7.   
  8.         var stdQuery = (from d in context.Student  
  9.                             where d.Course == Courses.MTECH  
  10.                             select d).FirstOrDefault();  
  11.   
  12.         Console.WriteLine("StudentID: {0} Name: {1}", stdQuery.ID, stdQuery.Name);  
  13.     }  

Now, the brand new Enum support allows you to have enum properties in your entity classes; see:
 
 
Step 6: Complete Code
 
Find the complete code of my console application as the following:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data.Entity;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.    
  8. namespace ConsoleApplication3  
  9. {  
  10.     public enum Courses  
  11.     {  
  12.         BCA, MCA, BTECH, MTECH  
  13.     }  
  14.    
  15.     public partial class Students  
  16.     {  
  17.         public int ID { getset; }  
  18.         public string Name { getset; }  
  19.         public Courses Course { getset; }  
  20.     }  
  21.    
  22.     public partial class StudentContext : DbContext  
  23.     {  
  24.         public DbSet<Students> Student { getset; }  
  25.     }  
  26.    
  27.     class Program  
  28.     {  
  29.         static void Main(string[] args)  
  30.         {  
  31.             using (var context = new StudentContext())  
  32.             {  
  33.                 context.Student.Add(new Students { Name = "Abhimanyu", Course = Courses.MTECH });  
  34.                 context.SaveChanges();  
  35.    
  36.                 var stdQuery = (from d in context.Student  
  37.                                     where d.Course == Courses.MTECH  
  38.                                     select d).FirstOrDefault();  
  39.    
  40.                 Console.WriteLine("StudentID: {0} Name: {1}", stdQuery.ID, stdQuery.Name);  
  41.             }  
  42.         }  
  43.     }  

Step 7: Find Database
 
When you run the application the first time, the Entity Framework creates a database for you. Because we have Visual Studio 2012 installed, the database will be created on the LocalDB instance. By default, the Entity Framework names the database after the fully qualified name of the derived context (for this example that is ConsoleApplication3.StudentContext).
 
 
Note, that if you make any changes to your model after the database has been created, you should use Code First Migrations to update the database schema, watch this.
 
I hope you like it. Thanks.