Reputation: 172
I am using Entity Framework Code with Code First development approach and a PostgreSQL Database. One of my classes has a enum property. This works out quite well.
However, when I took a look at the database I noticed that in the database the enum was actually stored as an integer, not as an enum as I had expected. So if you now directly query data from the database, you only see the integer and you have no clue what it means.
I have seen here and here that you can use a lookup table or store the enum as string to circumvent this, but this seems a bit unnatural IMHO, considering that you could also just could store the enum in the database.
So is there any way to store enums as enum datatype in the database? If no, why not?
Upvotes: 4
Views: 3743
Reputation: 84
you can check the https://www.postgresql.org/docs/9.1/datatype-enum.html for the enum type in postgres sql db, as for ef core,
can use modelBuilder.HasPostgresEnum("schema", "typename", new[] { "enum1", "enum2" })
, to let efcore know what to do for the enum type, the refences is here https://www.npgsql.org/efcore/mapping/enum.html?tabs=tabid-1
Upvotes: 4