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; }

}


8 Comments

  • Hi, can you explain more about this?

  • 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.

  • 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 )

  • =) 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.

  • 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!

  • 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();

  • 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.

  • 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.

Comments have been disabled for this content.