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.
After creating a application I’ve added Entity framework via Nuget package.
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 .
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.
After creating a application I’ve added Entity framework via 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.
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
0 comments:
Post a Comment
Your feedback is very important to me. Please provide your feedback via putting comments.