16

I can find a bunch of tutorials on how to implement enum support for model first and code first like these:

http://msdn.microsoft.com/en-us/data/jj248772.aspx

http://msdn.microsoft.com/en-us/data/hh859576.aspx

Can anyone explain to me or provide me with some instructions on how to properly implement enum support for database first applications. I'd imagine I'd have to start in the edmx designer and right click one of my columns in the tables and convert to enum? Thanks for any advice. I'm using .NET 4.5 and Enity Framework 5.0

TMan
  • 4,044
  • 18
  • 63
  • 117
  • btw, you can use code first against existing Db http://msdn.microsoft.com/en-us/data/jj200620 – phil soady Jul 19 '13 at 07:29
  • The idea is to have number-based representation in the data base like `short` and create your `enum` with implicit numbers e.g. `public enum Gender : short { NotSet = 0, MALE = 1, FEMALE = 2 }` and also add the attribute `[EnumDataType(typeof(short))]` to the field of your entity. – Jaider Jan 31 '15 at 00:11

3 Answers3

14

What I wound up doing is changing the column type to int in the database then I went into the edmx and updated model from database, then I went and added a enum type and then changed the column type in the edmx designer to be of type of that enum. Reupdated the poco's and everything Works good!

TMan
  • 4,044
  • 18
  • 63
  • 117
  • I am in a similar situation and would really appreciate more details on how you solved this. I have a table in the database that stores "types". It has two columns: ID and TypeName. Assuming you have a similar table in your case, what did you change exactly in the database? Then when you say you added an enum type to the edmx, how do you keep that up-to-date with the database when the database changes? – Kappacake Oct 03 '17 at 10:23
  • Yeah, maybe it was different at the time, but when I try this, there is a drop down of available types for the property and my enum is not one of them (none of my own types are) – xr280xr Apr 12 '18 at 15:18
  • If you end up here and wonder how to do it, there is a context menu item called “Convert to Enum” when you right click a Property in the EF Designer. Here is a screenshot from Microsoft: https://learn.microsoft.com/ef/ef6/modeling/designer/data-types/enums#add-an-enum-type So it’s acually real easy. The dialog creates the Enum for you. A generated file for it will end up with the other entities. – BenderBoy Feb 16 '22 at 00:03
12

You should probably read this as well.

Enum Types are not created in your model via Database-First actions

  • When you create an EDM from an existing database, Enums are not defined in your model.
  • Update Model from Database will preserve your declaration of Enum types, but again, will not detect Enum constructs from your database.

I've seen a couple of solutions that use T4, but in my specific case its just adding unneeded complexity to my project. So I will give in and so the enums code-first.

Community
  • 1
  • 1
dorphalsig
  • 689
  • 1
  • 10
  • 27
  • 4
    I agree with unneeded complexity. I have many enums in my model and reconfiguring types whenever tables get dropped/readded is really annoying, especially when your enum types are external. I am also "giving in" and using enums code-first. Maybe in the future we will have better database-first enum support. – jamesSampica Nov 02 '13 at 14:04
  • Update Model from Database does not always preserve enums. Fortunately, your compiler should catch when the designer replaces the enum declaration with an int. And yes, that's a "when", not an "if". – Suncat2000 May 20 '21 at 20:02
4

Here is my wild guess: After you generate your .edmx file from your database, you can follow the guide in the following link: http://msdn.microsoft.com/en-us/data/jj248772.aspx

mostruash
  • 4,169
  • 1
  • 23
  • 40