Home > Dotnet 4.5, Entity Framework > Getting Started with Entity Framework 5

Getting Started with Entity Framework 5

I finally took the plunge and installed Visual Studio 2012 and Entity Framework 5.Before I delve into any EF 5 code a few things I want to clarify about it.

Entity Framework 5 only comes with Visual Studio 2012 and you can’t get it using Visual Studio 2010, even if you try to use Nuget package manager it will install Entity Framework 4.4 but will report that it has installed Entity Framework 5.0

So if you run the following command

PM> Install-Package EntityFramework

and look into the version property of Entity Framework assembly it will still be and that was a bit of a gotcha for me.

I recommend you install Visual Studio 2012 Web Express if VS2012 is not available and currently there is no Visual Studio Express Edition for Windows 7 so no “Console Application” for the time being.

I think this must be some sort of marketing strategy from Microsoft to force users to upgrade to Windows 8 but in my opinion it is not a good thing. I work mainly on web technologies so it does not matter much to me but out there many people who would like to play with it and write some simple console application and they can’t.

Another thing which was sore to my eyes was the light color visual theme for visual studio it really looks ugly and I recommend change it from light to dark by doing Tools > Options > Environment > General > Visual Experience , trust me it will look and feel much nicer.

Visual Studio 2012 Web Express with Dark theme

See i told it does looks nice and shiny and since I had worked with Expression blend and Sketch flow in the past I actual like this theme.So enough of the intro and lets get back to EF 5

Let’s run the Install package command from VS 2012 as EF is hard wired up with .Net Framework 4.5.

Entity Framework installed from Nuget package manager

So the feature I am going to explore today is a much awaited feature and that is the support for enumerations and I will use a simple example of a Real Estate Property
as shown below.

Real Estate Property Class Diagram

As you can see a I have a domain object called Property which holds information of a real estate property.It also two enumeration attributes which are 1) The type of property 2) The Energy rating for a particular property.And this is how the code looks like.


public class Property
    public int Id { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public PropertyType PropertyType { get; set; }
    public EnergyRating EnergyRating { get; set; }
    public string Suburb { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }


public enum PropertyType


public enum EnergyRating
    Poor = 0,
    Average = 1,
    Good = 2,
    Excellent = 3

And lets write a unit test to see whether we can create the database or not.

public void Should_be_able_to_create_RealEstateContext_database()
    var context = new RealEstateContext();

So far so good and when you inspect the database tables you will see the tables have int as their data type for the enum columns,lets go ahead and add some data into the table.

public void Should_be_able_to_add_properties()
    var property1 = new Property
        AddressLine1 = "333 Baker Street",
        PropertyType = PropertyType.Residential,
        EnergyRating = EnergyRating.Excellent,
        Suburb = "Suburb1",
        State = "ACT",
        ZipCode = "2100"

    var property2 = new Property
        AddressLine1 = "444 Laker Street",
        PropertyType = PropertyType.Commercial,
        EnergyRating = EnergyRating.Average,
        Suburb = "Suburb2",
        State = "ACT",
        ZipCode = "2110"

    var property3 = new Property
        AddressLine1 = "404 Not Found Street",
        PropertyType = PropertyType.Governmental,
        EnergyRating = EnergyRating.Excellent,
        Suburb = "Suburb2",
        State = "ACT",
        ZipCode = "2120"

    var context = new RealEstateContext();


And as you can see all the three new property data is added to the database.Now lets write another test to retrieve this data using some LINQ and see if it all comes through.

public void Should_be_able_filter_by_energy_rating()
    var context = new RealEstateContext();
    var query = from p in context.Property
                where p.EnergyRating == EnergyRating.Excellent
                select p;
    var result = query.ToList();
    Assert.IsTrue(result.Count > 0);

As you can see I am trying to retrieve properties which have an energy rating of excellent and this test also passes through flying green color.

Entity Framework takes care of all the mapping, storing and retrieving of enum types.It also support both specified values or just as a type enums. For example in the above 2 enums theenergy rating type had specified values and EF 5 worked it out automatically how to map this values and store them in the database.

Below is the returned data in the watch window.

All the properties with Energy Rating Excellent

  1. Shibu Narayanan
    September 12, 2012 at 9:23 am

    The images are not visible… I believe it is first few blogs for EF 5 .. correcting the same might be of g8 help.. 🙂

    • September 12, 2012 at 7:56 pm

      Hi Shibu,
      I tested on all major browsers and it works for me. It could be your browser settings or something.

  2. February 23, 2013 at 11:27 am

    Hi prashantbra,

    Nice and simple explanation about enumerations support of EF 5.
    Good job.Keep it up.

  3. Manoj
    September 26, 2014 at 3:59 am

    Very nice and simple, really enjoyed reading your blog. Thanks.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: