What we do, and this might not be the best way to do so, is actually store the value of the enum in the database. Then, in code, we have an interface that mimics the table, that however has the enum instead of the int. Then we extend the entity class by giving it the interface.
It's extra work, but we do so because we cast the entity to a dto object anyways, but might work for you
Example
Let's assume in the table Person
we only have the gender
create table Person
(
Gender int not null
--other fields
)
Then what we do is generate an interface like
public interface IPersonEssential
{
Gender Gender {get;set;}
// ... Other properties
}
Where Gender
is the enum you propose in the OP, "Essential" is something we use to identify these objects that mimics the table, just use whathever convention you have already
Then we extend the EF class by implementing the interface, here we need to convert the int to enum and vice-versa,
a most basic way to do so would be:
public partial class Person : IPersonEssential
{
Gender IPersonEssential.Gender { get =>(Gender)Gender; set => Gender = (int)value; }
// ... Other properties that don't match the table in type like other enums
}
which would break if unknown enum values are on the database, but you can make a simple library method to convert the int to an enum which fallbacks to a default or whethever you might need
What we also do, which might be urelated to your case, is then make an extension method to cast the entity like:
public static T CastInto<T>(this IPersonEssential input, T output) where T : IPersonEssential
{
output.Gender = input.Gender;
// .... other properties
return output;
}
which you can use to convert an entity in a dto object if it has that interface.
we even made a tool to autogenerate this method : link
Hopefully it might be helpful, of course there are other approaches to the problem, or variations on this one.
For example you might this way be able to put "M" and "F" on the database and use the enum in code if you make an appropiate conversion in the EF class implementation of the interface.
You won't be able to use the enum directly in linq to entities, but you can still use it in code like:
_db.Users.Where(x=>x.Gender == (int)Gender.Male).First();