14

I am using Entity Framework 6, just release, and need to:

1 - Map a table column to an Enum;

2 - Map a lookup table (has two columns: Id and Name) to an Enum.

Is this possible in Entity Framework 6?

Thank you, Miguel

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

15

You typically don't map table to an enum type. You just define an enum type based on what you have in your lookup table and use it without including these tables in the model. For instance for the Northwind.Categories table:

ID  Name            Description
1   Beverages       Soft drinks, coffees, teas, beers, and ales
2   Condiments      Sweet and savory sauces, relishes, spreads, and seasonings
3   Confections     Desserts, candies, and sweet breads
4   Dairy Products  Cheeses
5   Grains/Cereals  Breads, crackers, pasta, and cereal
6   Meat/Poultry    Prepared meats
7   Produce         Dried fruit and bean curd
8   Seafood         Seaweed and fish

You would create the following enum type

public enum Categories
{
    Beverages = 1,
    Condiments = 2,
    Confections = 3,
    Dairy_Products = 4,
    Grains_Cereals = 5,
    Meat_Poultry = 6,
    Produce = 7,
    Seafood = 8,
}

(make sure that enum values correspond to the values in your database) and you would use it in your app without including the Categories table - i.e. you would use this enum type as the type of the properties that are foreign keys to the Categories table in the database. Alternatively - e.g. if you need descriptions - you would create an entity corresponding to the Categories table and use the enum (as defined above) as the key property type. Then again you would use the enum type for all properties that in the database are foreign keys to the Categories table.

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Pawel
  • 31,342
  • 4
  • 73
  • 104
  • 6
    This seems error prone in that the table and the enum can easily get out of synch. It would be nice if Code First and Migrations included mapping the NET enum to a table as discussed [here](http://stackoverflow.com/a/11173522/553593) – subsci Jun 30 '14 at 05:32
  • Since there is no hard link between your C# enum type and the values in the database there is always a risk that things will go out of synch. – Pawel Jul 01 '14 at 08:07
  • @subsci - There's a [nuget package for that](https://www.nuget.org/packages/ef-enum-to-lookup). Unfortunately, I'm looking to go the other direction so it doesn't help me here. – Bobson Aug 13 '15 at 18:42
  • I do not think this is a good advice, since the developer is responsible for manually binding the the ids on the table to the numeric values on the domain. – Gerardo Lima Mar 20 '17 at 11:44
  • Well, this is how things work. It's up to the developer to decide if they want to use it or not. It also clearly answers the question. – Pawel Mar 20 '17 at 17:24
  • 1
    requiring enum values to match table ids is halfway to maintainability headaches; do yourself a favor and avoid it. – Gerardo Lima Mar 30 '17 at 13:12