Entity Framework June 2011 CTP–enums!

 

The ADO.NET team at Microsoft just announced the release of the Microsoft Entity Framework June 2011 CTP.

There are a few nice features in there – one that I have been looking forward to is the enum support. Until now EF could not store enum values in the DB – the obvious way around this was to have a wrapper property in your entity class which exposed the enum value as an int or  whichever data type your enum is based on. This works but it is a bit messy, so baked in enum support is a welcome feature.

My current work with Entity Framework is mostly based around code first so I decided to create a quick sample to see how enum support worked in code first.

First I installed the bits from here http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=26660. I actually only installed the EF_JUNE_2011_CTP.msi as I didn’t need any designer or tools support for this quick test – it’s called code first for a reason Smile

image

 

I fired up Visual Studio and created a new console app called EFEnumSupportTest. I like console apps for investigating new UI agnostic framework features as there’s barely any extraneous UI code to distract you from what you are trying to investigate.

To use the EF CTP you need to change the target framework for your app to the June 2011 CTP in the properties dialog for the project.

 

TargetFramework

 

I added a reference to the EF CTP dll which on my machine was here:

 

C:\Program Files (x86)\Microsoft Entity Framework June 2011 CTP\bin\.NETFramework\System.Data.Entity.dll

 

Then the code. As this was a simple test I put all the code in the Program.cs file

 

Here is the whole file…

 

using System.Data.Entity;

namespace EFEnumSupportTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var db = new EnumTestContext();
            db.Cars.Add(new Car {Make = "Bentley", CarType = CarType.Saloon});
            db.Cars.Add(new Car {Make = "Jaguar", CarType = CarType.Saloon});
            db.Cars.Add(new Car {Make = "Aston Martin", CarType = CarType.Saloon});
            db.Cars.Add(new Car {Make = "Land Rover", CarType = CarType.Estate});
            db.SaveChanges();
        }
    }

    public class Car
    {
        public int Id { get; set; }
        public string Make { get; set; }
        public CarType CarType { get; set; }
    }

    public enum CarType
    {
        Convertible = 1,
        Saloon = 2,
        Estate = 3
    }

    public class EnumTestContext : DbContext
    {
        public IDbSet<Car> Cars { get; set; }
    }
}

Ctrl+F5 and everything works, database created, data in database. (By default this uses .\SQLEXPRESS – this can be changed in the config file of course)

 

CropperCapture[8]

Nice. Code first really does make things easy and enum support is a welcome addition. Hopefully custom conventions, second level caching and migration support will make it in there soon!

Add a Comment