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 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 in the project as well.

 

Step 3: Create Model

 

Now, time to define a new model using Code First Approach.

 

    public enum Courses

    {

        BCA, MCA, BTECH, MTECH

    }

 

    public partial class Students

    {

        public int ID { getset; }

        public string Name { getset; }

        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.

 

    public partial class StudentContext : DbContext

    {

        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:

 

    static void Main(string[] args)

    {

        using (var context = new StudentContext())

        {

            context.Student.Add(new Students { Name = "Abhimanyu", Course = Courses.MTECH });

            context.SaveChanges();

 

            var stdQuery = (from d in context.Student

                                where d.Course == Courses.MTECH

                                select d).FirstOrDefault();

 

            Console.WriteLine("StudentID: {0} Name: {1}", stdQuery.ID, stdQuery.Name);

        }

    }

 

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:

 

using System;

using System.Collections.Generic;

using System.Data.Entity;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ConsoleApplication3

{

    public enum Courses

    {

        BCA, MCA, BTECH, MTECH

    }

 

    public partial class Students

    {

        public int ID { getset; }

        public string Name { getset; }

        public Courses Course { getset; }

    }

 

    public partial class StudentContext : DbContext

    {

        public DbSet<Students> Student { getset; }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            using (var context = new StudentContext())

            {

                context.Student.Add(new Students { Name = "Abhimanyu", Course = Courses.MTECH });

                context.SaveChanges();

 

                var stdQuery = (from d in context.Student

                                    where d.Course == Courses.MTECH

                                    select d).FirstOrDefault();

 

                Console.WriteLine("StudentID: {0} Name: {1}", stdQuery.ID, stdQuery.Name);

            }

        }

    }

}

 

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.

Up Next
    Ebook Download
    View all
    DevOps
    Read by 0 people
    Download Now!
    Learn
    View all