34

Is there a standard way to using Enums in EF code-first? There seems to be some examples making use of a wrapper class for the enum.

However, I would like to be able to define the enum and have the enum values also seeded into the database using the database initializer. There doesn't seem to be much point in defining the enum and creating a wrapper, if I then have to seed the database table manually from the enum.

jaffa
  • 26,770
  • 50
  • 178
  • 289

3 Answers3

71

Unfortunately, enums are not natively supported on EF 4.1. Here's one rather known article on how to deal with them: Faking enums on EF 4. It does, however, require a wrapper.

There's a simpler way to map enums in EF 4 however: just create an int property on your class to represent the int value of the enum. That's the property that EF should map, then have a "mini wrapper" property to allow you to use the enum.

public class Appointment 
{   
    public int ID { get; set; }
    public string Description { get; set; }

    // This property will be mapped
    public int DayOfWeekValue { get; set; }

    public DayOfWeek Day
    {
        get { return (DayOfWeek) DayOfWeekValue; }
        set { DayOfWeekValue = (int) value; }
    }
}

public enum DayOfWeek
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

On generating the database, EF will happily ignore any type it doesn't know how to map, but the int property will be mapped.

Note: This is taken directly from my answer to another enum and EF question: EF 4.1 Code First - map enum wrapper as complex type

Community
  • 1
  • 1
Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101
  • what about the seeding of the types in the database using codefirst/initialisers. Do I need to manually tie the enums with the insertions myself? – jaffa Jun 14 '11 at 13:40
  • @jaffa - Could you add some code on how you'd ideally like to be able to initialise the db? Then we can work from there. – Sergi Papaseit Jun 14 '11 at 13:42
  • @Sergi - Before I do, maybe I'm misunderstanding something. Your Color type, is this something internal to your code only or is there an underlying table in the database with 1-Red, 2-Blue, 3-Green etc...? – jaffa Jun 14 '11 at 14:07
  • @jaffa - It's the standard `Color` enum of .NET; just used it as an example, but that would be a plain old `enum` you create in code. I'll change de example a little to make that clear. – Sergi Papaseit Jun 14 '11 at 14:17
  • @Sergi - so does the enum have a backing store in the database with the days of the week? – jaffa Jun 14 '11 at 14:57
  • @jaffa - No, not at all, the mapping takes place through the `int` property. The `int DayOfWeekValue` is stored in the database, and in code you can just use the `Day` property, which will convert the `int` (from the database) to a `DayOfWeek` `enum` value. – Sergi Papaseit Jun 14 '11 at 16:52
  • How would you localize this? so that on the view it would display Lunes instead of Monday.. without modifying anything in the database – nacho10f Aug 04 '11 at 19:35
  • @NachoF - Since you're only storing the `int` value in the database you just need to change the `enum` values :) – Sergi Papaseit Aug 04 '11 at 23:56
19

Now supported : http://blogs.msdn.com/b/adonet/archive/2011/06/30/announcing-the-microsoft-entity-framework-june-2011-ctp.aspx

The Microsoft Entity Framework June 2011 CTP introduces both new runtime and design-time features. Here are some of the new runtime features:

  • The Enum data-type is now available in the Entity Framework. You can use either the Entity Designer within Visual Studio to model entities that have Enum properties, or use the Code First workflow to define entities that have Enum objects as properties. You can use your Enum property just like any other scalar property, such as in LINQ queries and updates...

There are several new features for the Entity Framework Designer within Visual Studio:

  • The Entity Designer now supports creation of Enums, spatial data-types and table-value functions from the designer surface...
gnat
  • 6,213
  • 108
  • 53
  • 73
VinnyG
  • 6,883
  • 7
  • 58
  • 76
  • 5
    Just so you know, it's not supported in EF 4.2 (witch is available in NuGet) since June 2011 CTP is built with .net 4.5 feature so you have to change the scope of your application as explained in the link below. – VinnyG Oct 20 '11 at 13:15
-1

I Wrote a post about that. You can use Code First Migrations to add your enum values to the database. Look here: http://linqto.net/blog/2012/10/entity-framework-code-first-and-enum-support/

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30