Best method to store Enum in Database

前端 未结 10 1945
挽巷
挽巷 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:13

    I'm not sure if it is the most flexible, but you could simply store the string versions of them. It is certainly readable, but maybe difficult to maintain. Enums convert from strings and back pretty easily:

    public enum TestEnum
    {
        MyFirstEnum,
        MySecondEnum
    }
    
    static void TestEnums()
    {
        string str = TestEnum.MyFirstEnum.ToString();
        Console.WriteLine( "Enum = {0}", str );
        TestEnum e = (TestEnum)Enum.Parse( typeof( TestEnum ), "MySecondEnum", true );
        Console.WriteLine( "Enum = {0}", e );
    }
    

提交回复
热议问题