Synch enum with static data from database

I have a list of delivery status codes. And when I save the delivery data to the database, it is stored with a foreign key in a table containing the same data (i.e. the same delivery codes)

What is the best strategy for keeping the enumeration in sync with the data in the database?

Do you remember adding to the enum when new code is added to the database?

Or load data into a dictionary when the application starts? And use a dictionary instead of an enumeration? Although that means I don't have a strongly typed representation of the data, which is what I definitely want.

Or something else?

The data is not very volatile, but new codes are added after each blue moon

Would be grateful for any suggestions.

thank

0


source to share


2 answers


I am using T4 templates in Visual Studio 2008. That way I can get the code to generate at build time and it generates Enums for every table I want.

A good starting point would be the Hilton Geesenough sample, which I believe answers exactly your question.



A more interesting approach, from a different angle, would be to use SQL views to emulate enums in the database. This way you keep the database in sync with C # code (or others). You can find a great article about this on Oleg Sykh's blog, here .

+4


source


When I use an enum in code, I usually store the formatted name as varchar in the database rather than storing the table of the enum values ​​in the database. I realize this is not as normalized as I would like, but I find it better than trying to keep the database and my correspondence in sync. All that is needed is the insert / update formatting and parsing of choice to restore the value back to the enum.



I only do this when I think the enumeration will be fixed - although I did infrequent updates too. If I believe that the data is likely to be updated regularly, I will not use an enum and will have a separate table in the database with foreign key references.

0


source







All Articles