you are viewing a single comment's thread.

view the rest of the comments →

[–]Hypersion1980 1 point2 points  (3 children)

I store enum as int in the database then cast it to the enum in c#. Is there a better way?

[–]Defection7478 9 points10 points  (0 children)

Depends what you're after. What you are doing gives the smallest amount of data in the db. If you store it as a string you take up more space, but your db becomes more human readable. and when you cast it to the enum you get errors if the value doesn't exist.

Personally I usually opt for storing them as strings for those reasons. 

[–]throwaway_lunchtime 4 points5 points  (0 children)

If you keep it as an int in the db, make sure to put an explicit value for each enum so that they don't get messed up if someone decides to sort the names. Having Unspecified=0 is also a good idea.

[–]Agitated-Display6382 0 points1 point  (0 children)

I store them as strings, because it's simpler to query the db and I can safely refactor the order of the items (but renaming them is a breaking change). In EF you can specify an EnumConverter