I saw the light Enum in ADO.NET Entity Framework v1 - Alexey Zakharov's Blog

Enum in ADO.NET Entity Framework v1

There is no direct support for enum type in ado.net entity framework v1. But it is rather easy to solve this problem.

All u need to do is to create int property with private getter and setter and then add public property to your partial class, which type is enum:

public YourEnumType YourEnumTypeProperty

{

get { return (YourEnumType) InnernumTypeProperty; }

set { InnerEnumTypeProperty = (int) value; }

}


Published Thursday, September 18, 2008 12:30 AM by brainbox

Comments

# re: Enum in ADO.NET Entity Framework v1

Sunday, December 14, 2008 10:58 AM by Reski

Hi, can you explain more about this?

# re: Enum in ADO.NET Entity Framework v1

Monday, December 15, 2008 3:42 AM by brainbox

Hmm.. First of all you create a private int property. Then you will add public property of your enum type which will convert enum value to int and set to your private int property.

But this feature is not so good as i later found out, because you won't be able to use this public enum property inside linq to Entity query.

# re: Enum in ADO.NET Entity Framework v1

Friday, January 16, 2009 3:33 PM by Craig

This implies that your underlying data type is and integer. What if it is a string I want to map to enum values?

Also how would you go about using the enum property within a LINQ query? (e.g. ... where myentity.color = colorenum.green )

# re: Enum in ADO.NET Entity Framework v1

Saturday, January 17, 2009 2:16 AM by brainbox

=) Look at my comment above. It is not possible to use them inside queries. Also enum properties will be a big problem if u will use ado.net data services. I'm thinking of better solution for my new project and may be right about it soon =)

PS: In Telerik Open Access ORM enum properties can be used inside linq queries.

# re: Enum in ADO.NET Entity Framework v1

Wednesday, April 08, 2009 11:45 AM by John Bubriski

This is a great tip, however, LINQ seems to have issues if you attempt to use the enum field in a LINQ query.  THe workaround for this is to leave the "internal" int field exposed, and use that when querying using LINQ.

Just cast the Enum back to an int when doing the comparison.  HOWEVER, it looks like you need to the the cast into a temporary variable, BEFORE the query.  if you do the cast IN the query, it throws an exception.  Although, maybe you could wrap the whole cast operation in parentheses.  Thanks!

# re: Enum in ADO.NET Entity Framework v1

Tuesday, April 14, 2009 7:59 AM by Francois Hattingh

What I have done is create an extension method on my enum which returns the actual entityobject for me. E.g. ToEnity(). This allows me to kind of work with enums, in the code and in the query.

I would have something like this.

Entity.NavigationProperty = MyEnumCollection.Enum.ToEnity();

# re: Enum in ADO.NET Entity Framework v1

Tuesday, August 18, 2009 8:03 AM by weitzhandler

They should of make in the selection types a way to choose the col type and include there a list of all the .net enums including proj enums, so you don't have to declare an extra property which you can't linq with it.

# Entity Framework - part 2

Sunday, October 25, 2009 12:05 PM by שלמה גולדברג

נמשיך בלימוד על Entity Framework. בפרק הזה נייצר את הישות הראשונה שלנו כמובן שנעשה זאת ידני - לאחר שנבין

# re: Enum in ADO.NET Entity Framework v1

Tuesday, April 27, 2010 7:43 PM by Michael Bunney

I rely on being able to use enum properties in LINQ to SQL models.  This will be a big obstacle in moving to Entity Framework.

# dotConnect Team???s Blog » Blog Archive » Enumeration Support

Pingback from  dotConnect Team???s Blog  » Blog Archive   » Enumeration Support

Leave a Comment

(required) 
(required) 
(optional)
(required)