Enum Support (Code First) in Entity Framework 5

Entity Framework 5 brings number of improvements and Enum Support in Code First is one of them.

In this post I’ll follow the simple steps to develop console app with Entity Framework Code First and then explore Enum.

Step 1: Create New Project

Create a new console application File > New > Project > Visual C# > Console Application.

Step 2: EF5 from NuGet

At very first, we need to enable the Entity Framework 5 for this console project from NuGet Package Manager. For this, in ‘Solution Explorer’ right click on 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 reference to this DLL by 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 { get; set; }
        public string Name { get; set; }
        public Courses Course { get; set; }
    }

Please note, the above code defines an ‘enum’ by 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 { get; set; }
    }

Note to use a namespace ‘using System.Data.Entity;’ on the top.

Step 5: Data Handling

Now, I all set to retrieve data and for this we need some code in Main() method.

    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.



Step 6: Complete Code

Find the complete code of my console application.

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 { get; set; }
        public string Name { get; set; }
        public Courses Course { get; set; }
    }

    public partial class StudentContext : DbContext
    {
        public DbSet<Students> Student { get; set; }
    }

    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.

Comments

Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System