Search code examples
c#.netenumsstaticconstants

Enum vs Static class in database table


I have to store in database

[Column 1], [Column 2-start datetime]
Version1_1,   01-01-2011
Version1_2,   01-01-2011
Version1_3,   01-01-2011

… I have a class with two fields: the version name and the datetime (+id). The version name represents the first column. It can be an enum or a static class with constants.

Regarding almost any scenario I understand enums are better. But then, The DB will store integers in the first column instead of string values. Which gives me a feeling of uncertainty.

Are the enums still the best option in this scenario? I don't see disadvantages in lacking clean string values in [Column 1] in database.


Solution

  • If you store the values as integers in the database, you have several advantages:

    • Less storage space required
    • Easier querying without taking string comparisons into account
    • Better query performance because integer comparisons are much faster than string comparisons

    A disadvantage on the database side is of course the reduced readability, but considering the advantages, I'd prefer integers in the database.

    On the C# side, enums let you have the best of both worlds: integers inside and at the same time text identifiers when working with the values.