Enum Support in Entity Framework 5.0 using Model First and the EF Designer

In this post I will show you with a hands-on demo the enum support that is available in Visual Studio 2012, .Net Framework 4.5 and Entity Framework 5.0.

In this post I will use the Model First paradigm available to us so I can demonstrate the support that is available for enums in the EF Designer.

This model was firstly introduced in EF version 4.0 and we could start with a blank model and then create a database from that model.When we made changes to the model , we could recreate the database from the new model. 

Enum support is a new feature of EF 5.0 and .Net 4.5. So you must have that installed.VS 2012 targets .Net 4.5 by default.

You can have a look at this post to learn about the support of multilple diagrams per model that exists in Entity Framework 5.0.

You can also have a look at this post, Enum Support in Entity Framework 5.0 Code First.

We will demonstrate this with a step by step example. I will use Visual Studio 2012 Ultimate. You can also use Visual Studio 2012 Express Edition.

Before I move on to the actual demo I must say that in EF 5.0 an enumeration can have the following types.

  • Byte
  • Int16
  • Int32
  • Int64
  • Sbyte

Let's begin building our sample application.

1) Launch Visual Studio. Create an ASP.Net Empty Web application. Choose an appropriate name for your application.

2) Add a web form, default.aspx page to the application.

3) Now we need to make sure the Entity Framework is included in our project. Go to Solution Explorer, right-click on the project name.Then select Manage NuGet Packages...In the Manage NuGet Packages dialog, select the Online tab and choose the EntityFramework package.Finally click Install.

Have a look at the picture below



4) Add a new project to your solution, a class library project.Name it ModelFirst.Remove the class1.cs file from the project.

5) Add a new item to your class library project, a ADO.Net Entity Data model. Choose a suitable name for it, e.g Football.edmx.

Have a look at the picture below


6) In the Entity Data Model Wizard select "Empty Model" and click Finish.


7) In the Entity Designer right click on the surface and Add a new entity. Give a name to the Entity e.g Footballer. Add the property names with the appropriate types.Always check that you have a Key Property selected.

I will add the properties FirstName,LastName,Age and Position. I will set the properties for all those Entity properties.

Have a look at the picture below


8) I will now convert the Position (which is of type Int32) property to Enum. I simple select it and righ-click on the option "Convert to Enum"

I set some values for my enumeration and then hit OK.

Have a look at the picture below


Also have a look through the Solution Explorer at the .edmx file that has been generated for us.Also note the T4 templates and the code they generate for DBContext and the the POCO class(es) that map to our entity(ies).

Have a look at the picture below


9)  Now we are ready to generate the database. First you have to go to the local instance of SQL Server and create an empty database.I named it Football.

Then I right click on the EF designer and select "Generate database from model". In the wizard (create a new connection to the SQL Server instance) choose the database you just created and click Next.

The DDL is created and finally hit Finish.This does not execute the script.

Have a look at the picture below


10) The Football.edmx.sql script is loaded in the VS 2012. Right-Click on the window and select "Execute". The script will be executed and all the database objects will be created in the SQL Server.

11) Make sure you add a reference to the ModelFirst class library project to the Web Forms Application project.I have decided in order to keep things tidy to have a separate project to host my data access layer.

Now we must copy the connections string from App.config of the ModelFirst class library project to the web.config


    <add name="FootballContainer" connectionString="metadata=res://*/Football.csdl|res://*/Football.ssdl|res://*/Football.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sqlexpress;initial catalog=Football;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />


12) In the Page_Load event handling routine of the default.aspx page, I will insert a record in the database and then print it in the screen.The code follows.It is a very simple piece of code and I expect you to know the basics of Linq to Entities queries.

Make sure you add this line using ModelFirst; at the beginning of the default.aspx.cs file.

protected void Page_Load(object sender, EventArgs e)

            using (var context = new FootballContainer())
                if (!IsPostBack)
                    context.Footballers.Add(new Footballer { FirstName = "Steven", LastName = "Gerrard", Age = 32, Position = FootballPositions.MidFielder });


                    var foot = (from player in context.Footballers

                                 select player).FirstOrDefault();




 13) I run my application. The record is saved in a transactional way in the database (context.SaveChanges();). Then the word "Midfielder" is printed out in the screen. The enum value was stored and retrieved from the database.

Have a look at the picture below where I show you the contents of the Footballers table.



 Enum support is great in VS 2012 and EF 5.0. Make sure you have EF 5.0 installed and referenced and that your project targets .Net 4.5.

Hope it helps!!!!

No Comments