Support for Enums
I would really like support for enums in the model. This should of course be available in the designer as well.
Support for mapping integer columns to enum types will be enabled by the update of the EF core libraries included in .NET 4.5.
However, in this release we won’t be addressing some of the other ideas mentioned in the comments, such as type conversions in property mapping and design-time support for reverse engineering lookup tables into enum types. Since we are closing this item now, we encourage you to add/vote for those suggestions in separate items.
23 comments
-
Brian
commented
We found a simple solution to this. For us we have reference data such as payment types, note types, .... We want to have an enumerated value for each. So in the database, we have an EnumKey column that is a string containing the enumerated value. Then on the entity that corresponds to the table we have a property of the Enumerated Type. And in the implementation of the property, we use the Enum.Parse() method to convert the string to the Enumerated value. Works great and you can use in in linq queries, and in business logic.
-
abatishchev commented
Agree.
-
Felipe Fujiy Pessoto
commented
OrderType is a Enum and at database Order.Type is a int, only that.
You dont need a table for OrderType. -
abatishchev commented
Felipe Fujiy Pessoto, if you don't need a table for enum, how are you going to store/configure a relation, e.g. order -> order_type?
-
Felipe Fujiy Pessoto
commented
Bruce Leggett, I think that Enums types dont generate tables.
And we dont need a table to store a Enum -
Bruce Leggett
commented
Manually updating Enums is worthless! Enums should be GENERATED from look up tables and vice-versa back to the database, even if the Enum tables have to follow a specific schema (i.e. Id, Name, DisplayName columns); however, it would be best if the developer can choose which columns to use for when generating the Enum names, values, and type.
-
David Nelson
commented
Even more than just enum support, I would like to see a table mapped as an enum (or even a complex type with properties), where all of the values are determined at design time, and EF does NOT join to the table at runtime, because it knows that the values are static.
-
David Nelson
commented
It is embarrassing that EF is supposed to be THE data access technology from Microsoft and it still doesn't have the most basic support for enums that every single other ORM has.
-
Sean Terry
commented
I must admit that with EF being the official way forward, I'm more than a little annoyed that I still can't do with EF what I was doing with Linq to SQL three years ago.
Code only was the most important one for me, which 4.1 has brought about. Next up is enumeration support.
-
Sławek
commented
It will be great if I could map enum to different values in database for different tables
-
Serge
commented
I would prefer a general value converter. Comparable to the value converters in WPF. This possibility could also Enums are handled.
-
Loupi
commented
You can already do this with a complex type.
See this post: http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/24838ff0-9045-4b87-aa39-777359d261e1 -
zhousongbai
commented
I hope this can be added
-
Moby Disk
commented
I would like this too - it doesn't have to be a grandiose solution that creates a table mapping the enum IDs to strings or anything. It just needs to do enough to cast the enum to an int and vice-versa. The rest would be icing on the cake,
-
shimmy
commented
I want to! Also support for types that have a constructor implementing a certain interface (or in VB that implements widening/narrowing conversion) that can convert and convert back from source type.
-
Anonymous
commented
i want !
-
Daniel F.
commented
would be great to have ability to map table as an enum ie. I have table OrderStatus with fixed values/rows (New, InProgess, Completed etc.); I'd like to work with enum OrderStatus witn enum members populated from OrderStatus table;
-
j2jensen
commented
I was surprised to find that I couldn't even use a static enum in a selector:
var dtos = entities.Select(e => new MyDto{Title = e.Title, Type = MyEnum.A});It seems like this much at least should be extremely easy to support, right?
-
Konstantin Tarkus commented
In addition to mapping enums support I would really enjoy having a way to map complex serializable types to xml columns:
http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1124827-map-xml-sql-datatype-to-a-strongly-typed-serializa -
Jon Miller
commented
I too would like to see support for this and was actually surprised that it isn't already supported in EF 4. What's in EF 4 is really nice, but, it's still missing a lot of basic features that have been in NHibernate for years, such as support for enums.