Rafael Romão
Rafael Romão

Reputation: 1798

How to work with Enums in Entity Framework?

What is the best way to work with Enums in Entity Framework?

Remarks: I'm using EF 3 and Firebird.

Upvotes: 53

Views: 40703

Answers (5)

Craig Stuntz
Craig Stuntz

Reputation: 126547

There is a somewhat better way to do it in EF 4. Unfortunately, it won't work in EF 1.

Here's another approach.

Update: Real enum support was added in the June 2011 EF CTP.

Upvotes: 27

Geoff
Geoff

Reputation: 4746

Update:
Entity Framework now supports Enums nativity.

Original:
This is one of those irritating things about EF. Will not be supporting it yet!

Or you can do something like:

public MyEnum MyEnumProperty  
{  
  get { return (MyEnum) InnerEnumProperty; }  
  set { InnerEnumProperty = (int) value; }  
}

But it makes me feel dirty.

Upvotes: 20

Christophe Lambrechts
Christophe Lambrechts

Reputation: 538

I had a similar problem and solved it by writing an extension on the entity through the partial class mechanism. I just added a property that does the casting of DB field already in the entity, in our case just an integer.

Only pitfall is to add an ignore serialization attribute, for example when using in combination with WCF.

Upvotes: 0

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102368

This question is a bit old, but let me point you to a more recent material since today we have a newer version of Entity Framework:

Video: Entity Framework 5 Enums and Moving Solution from EF 4.3 by Julie Lerman

I used this video today to catch up with enums in Entity Framework. It's a great step by step demonstration. Hope it helps you too.

There's also this introductory post on Entity Framework Design blog:

Enumeration Support in Entity Framework

Upvotes: 11

MemeDeveloper
MemeDeveloper

Reputation: 6782

I make heavy use of tables (with default values) in DB of the form

CREATE TABLE [dbo].[CommunicationPreferences]
(
    [ID] smallint NOT NULL,
    [SystemName] nvarchar(50) NOT NULL,
    [Description] nvarchar(200) NOT NULL,
)

And I drive my EF4 entities from the DB.

N.B. I use no views, SPROCS or SQL functions, no complex EF types, just direct table to entity mapping. Then extend my entity partial classes to add additional functionality to keep things DRY.

For Enums I have a simple T4 template, which I hand a list of tables (of the form above), the .tt file gets fired off whenever I update the EF model from the DB (or if I need to on demand), it grabs the data, and builds Enums e.g.

/// <summary> 
/// Enums For The dbo Schema
/// </summary>
public enum CommunicationPreferencesList : short
{
    /// <summary> 
    /// HTML Emails
    /// </summary>
    [EnumTextValue(@"HTML Emails")]
    HTMLEmail = 1,

    /// <summary> 
    /// Plain Text Emails
    /// </summary>
    [EnumTextValue(@"Plain Text Emails")]
    PlainEmail = 2,

    /// <summary> 
    /// Mobile Telephone
    /// </summary>
    [EnumTextValue(@"Mobile Telephone")]
    Mobile = 3,

    /// <summary> 
    /// Landline Telephone
    /// </summary>
    [EnumTextValue(@"Landline Telephone")]
    Landline = 4,

    /// <summary> 
    /// SMS
    /// </summary>
    [EnumTextValue(@"SMS")]
    SMS = 5,

}

Then when I am dealing with an FK ID column / Property on some entity e.g.

Users.CommunicationPreferenceID

I simply cast either the ID or the enum for the comparison. e.g.

CommunicationPreferencesList usersPreference = (CommunicationPreferencesList)currentUser.CommunicationPreferenceID;

if(usersPreference == CommunicationPreferencesList.SMS)
{
//send SMS
}
else if(usersPreference == CommunicationPreferencesList.Mobile)
{
//ring the phone
}

I then have some simple helpers to e.g. give the EnumTextValue from an enum instance e.g.

string chosenMethod = EntityHelper.GetEnumTextValue(CommunicationPreferencesList.Mobile);

 => "Mobile Telephone"

I find this simple, hassle free, transparent, easy to use, and for my purposes it works a treat and I am happy. I don't see the need to totally mask the numeric value in the DB / entity from the consuming code, I am quite happy to cast one or other of the values, and end up with pretty clean readable code, plus the nice little extra of the EnumTextValue which is generated from the [Description] field in the DB.

Upvotes: 6

Related Questions