Quantcast
Channel: .Net and SharePoint News » O/R Mapper
Viewing all articles
Browse latest Browse all 10

[EF] EF 5.0 : Use enums with Code First

$
0
0

With the latest version of Entity Framework, we are now able to use enums in our code. I will share here a simple sample on how enumerations work with EF. To do it, we will use Visual Studio 2012 and install EF from Nuget package.

First, we will create a simple Console Application project. Go to Visual Studio, select New Project and choose ConsoleApplication using .Net 4.5 framework. Give a name to your project. Mine is EnumConsole.

Console4.5

Ok, so now, we are going to create our class.

First, let’s create the enumeration. Create a new class file named Gender and add the following code :

public enum Gender 
    { 
        Female, 
        Male 
    }

Then, we are going to use a very simple class Person with 4 properties : Id, FirstName, LastName and Gender which will be the enum. Here is the code of the class :

public class Person 
    { 
        public int PersonID { get; set; } 
        public string LastName { get; set; } 
        public string FirstName { get; set; } 
        public Gender Gender { get; set; } 
    }

Now, we will create our context class. Create a new class file and add the following code :

using System.Data.Entity;

namespace EnumConsole 
{ 
    public class MyContext : DbContext 
    { 
        public DbSet<Person> Persons { get; set; }

        public MyContext() 
        { 
        } 
    } 
}

Everything is ready, we can use it. We will modify our console application to save a Person. When the code will be launched the first time, EF will create the database and should be able to manage our enumeration. Here is our code to save a new person :

static void Main(string[] args) 
{ 
    using (var context = new MyContext()) 
    { 
        Person person = new Person(); 
        person.FirstName = "Nadège"; 
        person.LastName = "Deroussen"; 
        person.Gender = Gender.Female; 
        context.Persons.Add(person); 
        context.SaveChanges(); 
    } 
    Console.WriteLine("Person saved!"); 
    Console.ReadLine(); 
}

Ok, so now, launch the application.

If everything went fine, you should see the message Person saved! appears on the console.

Now, let’s look at the database. The database should be named EnumConsole.MyContext if you’ve not modify the name of it. Here is its schema :

TablePeople

Open then the table definition. Here is the design of the table :

DesignTablePeople

We can see the enum is represented with a int value as we should have done if we’d have to do it ourself.

And to finish, let’s see the data :

TablePeopleData

Datas are as expected. If we add a male person, we should see the value ‘2’ in the gender column. This is a really good news EF now supports Enum because it was really a missing feature.

Now we have it, let’s use it. (I should have to migrate my own application to EF 5 now)

Happing coding Smile


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images