Enum Support for Entity Framework Database First

24,719

Solution 1

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!

Solution 2

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.

Solution 3

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

Share:
24,719
TMan
Author by

TMan

Updated on October 10, 2020

Comments

  • TMan
    TMan over 3 years

    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

  • jamesSampica
    jamesSampica over 10 years
    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.
  • Kappacake
    Kappacake over 6 years
    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?
  • xr280xr
    xr280xr about 6 years
    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)
  • Suncat2000
    Suncat2000 almost 3 years
    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".
  • BenderBoy
    BenderBoy about 2 years
    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: docs.microsoft.com/ef/ef6/modeling/designer/data-types/… 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.