How to save enum in database as string

In Entity Framework Core you can specify the built-in conversion.

If you have an enum type

public enum MyEnumType
{
    ...
}

and a model class with this property

public class EntityWithEnum
{
    public MyEnumType MyEnum { get; set; }
    ...
}

then you can add the built-in conversion

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<EntityWithEnum>()
        .Property(d => d.MyEnum)
        .HasConversion(new EnumToStringConverter<MyEnumType>());
}

More details here.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)