How to save enum in database as string

后端 未结 6 689
北恋
北恋 2020-12-29 02:59

This is my Model Class where we have a Type which could be a Zombie or Human

public class User
{
    public int ID { get; set; }
    public string Name { ge         


        
6条回答
  •  庸人自扰
    2020-12-29 03:39

    I had this problem as far as I remember and honestly I don't know why didn't MS add this feature (NH can do it like since always..).

    Any ways, what I usually did is use const strings classes like:

    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 - as simple as can be.

    Downsides - you loose type checking (though it could be enforced programmatically).


    So this time I tried to think of something more ambitious. So I took the concept described by Brian (which has some downsides when e.g. a given enum is used widely across the domain). And well.. I got the following working:

    A base component class to store the values:

    [ComplexType]
    public class DbEnum
    {
        public string _ { get; set; }
    
        public DbEnum()
        {
            _ = default(TEnum).ToString();
        }
    
        protected DbEnum(TEnum value)
        {
            _ = value.ToString();
        }
    
        public TEnum ToEnum()
        {
            return _.ToEnum();
        }
    
        public static implicit operator DbEnum(TEnum value)
        {
            return new DbEnum(value);
        }
    
        public static implicit operator TEnum(DbEnum value)
        {
            return value.ToEnum();
        }
    }
    

    ... which would be basically sufficient.. except EF doesn't support generic types...

    This means for every enum you have to have something like...

    public enum PrivacyLevel
    {
        Public,
        Friends,
        Private
    }
    
    public class PrivacyLevelEnum : DbEnum
    {
        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();
        }
    }
    

    Which gives you some boiler-plate that could be easily generated e.g. using T4 templates.

    Which finally ends you up with using:

    public class CalendarEntry : Entity
    {
    
        public virtual PrivacyLevelEnum PrivacyLevel { get; set; } = new PrivacyLevelEnum();
    
    }
    

    But since you have implicit conversion in place, class declarations are the only ones to be aware of the helper types.

提交回复
热议问题