Best method to store Enum in Database

前端 未结 10 1964
挽巷
挽巷 2020-12-12 12:42

What is the best method of storing an Enum in a Database using C# And Visual Studio and MySQL Data Connector.

I am going to be creating a new project with over 100 E

10条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 13:24

    If you need store in DB string values of enum field, better do like show below. For example, it can be needed if you are using SQLite, which don`t support enum fields.

    [Required]
    public string PhoneTypeAsString
    {
        get
        {
            return this.PhoneType.ToString();
        }
        set
        {
            PhoneType = (PhoneTypes)Enum.Parse( typeof(PhoneTypes), value, true);
        }
    }
    
    public PhoneTypes PhoneType{get; set;};
    
    public enum PhoneTypes
    {
        Mobile = 0,
        Home = 1,
        Work = 2,
        Fax = 3,
        Other = 4
    }
    

提交回复
热议问题