Sunday, March 22, 2015

Entity Framework Internals: Enum Support

In .NET world, We all are using Enums in our code. It’s makes our code more readable then using hardcoded numbers of strings. Entity Framework 5.0 or higher version has support for Enums. In this blog post we are going to see how we can use Enums with entity framework.

We are going to create a console application to see how it’s works. I’m going to create a console application for the same.

entity-framework-enum-support-console-application

After creating a application I’ve added Entity framework via Nuget package.


install-entity-framework-nuget-package

How to use Enum with Entity Framework:

Now everything is ready so let’s write some code to demonstrate how we can use Enums in entity framework. So I have created following Enum for department for Employee.
namespace EnumSupportEF
{
    public enum Department
    {
        Sales=1,
        Marketing =2,
        Admin=3
    }
}
Now Let’s create our employee class who belongs to some departments.
namespace EnumSupportEF
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Department Department { get; set; }
    }
}
Now our domains are ready so let's write our dbcontext class.
using System.Data.Entity;

namespace EnumSupportEF
{
    public class EmployeeContext : DbContext 
    {
        public EmployeeContext() :base("EmployeeConnectionString")
        {
            
        }
        public DbSet<Employee> Employees { get; set; } 
    }
}
Here I have used EntityFramework with my Test database so I have passed my connection string to database. Now let’s run write some code to create some employees.
using System;

namespace EnumSupportEF
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee employee = new Employee
            {
                EmployeeId = 1,
                FirstName ="Jalpesh",
                LastName  ="Vadgama",
                Department = Department .Admin
            };

            Employee anotherEmployee = new Employee
            {
                EmployeeId = 2,
                FirstName = "Vishal",
                LastName = "Vadgama",
                Department = Department .Marketing
            };

            //Code for adding Employees
            using (EmployeeContext employeeContext = new EmployeeContext())
            {
                EmployeeContext.Employees.Add(employee);
                EmployeeContext.Employees.Add(anotherEmployee);
                EmployeeContext.SaveChanges();
                Console.WriteLine("Employee Added Sucessfully");
            }
        }
    }
}
Here you can see that I have used Enums with my employee objects and once you run this application. You can see output of console application like following.

entity-framework-console-application-output

Now let’s check database. Let’s first check how columns are created.




















If you see column carefully, You can see Entity framework is smart enough to create a column of Department with int as We have used integers in our Department Enum.  Now let’s check data.


















And If you see data It has correctly inserted Department as our Enums selected at the time of employee creation. That’s it. It’s very easy to use Enums .

You can find complete source code of above blog post on following location at github -https://github.com/dotnetjalps/EnumSuportEntityFramework
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews