How to work with Enums in Entity Framework?

.NetEntity FrameworkEnums

.Net Problem Overview


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

Remarks: I'm using EF 3 and Firebird.

.Net Solutions


Solution 1 - .Net

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

Here's [another approach][2].

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

[1]: https://docs.microsoft.com/en-us/archive/blogs/alexj/ "Tip 23 – How to fake Enums in EF 4 " [2]: https://landman-code.blogspot.com/2010/08/adding-support-for-enum-properties-on.html [3]: https://docs.microsoft.com/en-us/archive/blogs/adonet/walkthrough-enums-june-ctp

Solution 2 - .Net

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.

Solution 3 - .Net

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

Solution 4 - .Net

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.

Solution 5 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRafael Rom&#227;oView Question on Stackoverflow
Solution 1 - .NetCraig StuntzView Answer on Stackoverflow
Solution 2 - .NetGeoffView Answer on Stackoverflow
Solution 3 - .NetLeniel MaccaferriView Answer on Stackoverflow
Solution 4 - .NetMemeDeveloperView Answer on Stackoverflow
Solution 5 - .NetChristophe LambrechtsView Answer on Stackoverflow