How to save enum in database as string

.net c# c#-4.0 entity-framework

Accepted Answer

As far as I can remember, I had this issue, therefore I'm not sure why MS didn't include this feature (NH can do it like since always..).

In any case, I typically used classes for const strings, such as:

public static class MyEnum
{
    public const string Foo = "Foo";
    public const string Bar = "Bar";
}

public class Client
{

    public string MyVal { get; set; }

    public Client()
    {
        MyVal = MyEnum.Bar;
    }

}

Cons: Too straightforward.

Cons: You forfeit type-checking (though it could be enforced programmatically).


I made an effort to come up with something more ambitious this time. I therefore adopted Brian's idea (which has some downsides when e.g. a given enum is used widely across the domain). And hey, I was able to make the following work:

a fundamental component type to hold the values:

[ComplexType]
public class DbEnum<TEnum>
{
    public string _ { get; set; }

    public DbEnum()
    {
        _ = default(TEnum).ToString();
    }

    protected DbEnum(TEnum value)
    {
        _ = value.ToString();
    }

    public TEnum ToEnum()
    {
        return _.ToEnum<TEnum>();
    }

    public static implicit operator DbEnum<TEnum>(TEnum value)
    {
        return new DbEnum<TEnum>(value);
    }

    public static implicit operator TEnum(DbEnum<TEnum> value)
    {
        return value.ToEnum();
    }
}

... which, if EF had generic types, would be essentially sufficient.

This means that you need something similar for each enum.

public enum PrivacyLevel
{
    Public,
    Friends,
    Private
}

public class PrivacyLevelEnum : DbEnum<PrivacyLevel>
{
    public PrivacyLevelEnum() : this(default (PrivacyLevel))
    {      
    }

    public PrivacyLevelEnum(PrivacyLevel value) : base(value)
    {
    }

    public static implicit operator PrivacyLevelEnum(PrivacyLevel value)
    {
        return new PrivacyLevelEnum(value);
    }

    public static implicit operator PrivacyLevel(PrivacyLevelEnum value)
    {
        return value.ToEnum();
    }
}

This provides some boilerplate that may be quickly produced, for example, using T4 templates.

Which ultimately leaves you using:

public class CalendarEntry : Entity
{

    public virtual PrivacyLevelEnum PrivacyLevel { get; set; } = new PrivacyLevelEnum();

}

However, class declarations are the only ones that are aware of the helper types because you have implicit conversion in place.

8
11/22/2016 11:25:35 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow