The most requested feature for Entity Framework has been Enum support for a very long time. At TechEd this year it was announced that Enums would be supported in the next version of the Entity Framework and in June the Entity Framework June CTP was released with Enum support as well some other enhancements.
In the past I dealt with Entity Framework not supporting Enum by having an int property on my model that represented the Enum’s id. My model also had a property of the Enum type that was marked as ignored so that EF did not try create a database column for the property. The setter of the property would set the Enum id field and the getter would convert the Enum id field to the Enum type and return it. If you are confused by that statement, like me, see the code below.
This gets the job done and really isn’t that messy, but with the new Enum support in the June CTP I can make things cleaner. To make use of the new Enum support all I have to do is declare a property of EngineLocationType in my model and EF will make all the connections for me.
So that is the new EF Enum support. The only issue I have with the implementation is that ideally I would like to be able to have an EngineLocationType table in my database that Entity Framework would create and populate based on my Enum definition and assign a foreign key relationship to EngineLocation on my Model entity. That would make the entire process complete and architecturally sound IMHO.
If you are looking for a code download this is part of the Entity Framework Code First Demo that I did in a previous blog post and the code is available on GitHub.
Keith is a Senior Software Engineer with Falafel Software. He has been developing software since 1999 specializing in web-based solutions primarily using the Microsoft stack. He has been a Microsoft MVP in ASP.NET since 2012.
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.