Should I store my Enums at database level or in the application logic (.NET)?

柔情痞子 提交于 2019-12-05 17:06:19

We add them in both places, we use code generator to keep in sync the values.

The main values are in the Enum but the template automatically generates a script to update the DB

We also add a property to our entities to expose an enum property instead of the integer that is saved to the DB

Edit: Adding an example of how to do it (In VB.NET:

    <EnumDbTableInfo("TableName", "keyColumn", "descColumn")> _
    Public Enum CasoImportacion As Short

        <Description("")> _
        Normal = 0

        <Description("The First Description")> _
        FirstRealValue = 1

        <Description("A second one")> _
        AnotherValue = 2

        <Description("Third Description")> _
        LastValue = 3


End Enum

Later an EnumHelper class take the classes that has the attribute EnumDbTableInfo and generates the script to update the DB

Best Regards

I generally use a foreign key (lookup) table in the db if I will need to join the tables to get a text description of the code for display to a user, say for a report, or ad hoc querying or something. If the value is strictly internal - that is, never displayed to anyone - or is used only in my code where I can translate it as needed, I would just put a constraint on the field and skip the foreign key table.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!