4

I have a database table that essentially contains different types of things. I'll use animals as an example. I have a table called AnimalTypes:

AnimalTypes
{
     ID:int,
     Name:string
}

I then populate it with:

1:Dog,
2:Cat,
3:Fish

I would like to then have some sort of C# object created that functions similar to this enum be entirely read from the database:

enum AnimalTypes
{
     Dog = 1,
     Cat = 2,
     Fish = 3
}

Is there a way to create an enum/class from a database table as described? I basically want to be able to reference things in the AnimalTypes table using intellisense and AnimalTypes.Dog as an example; I don't actually need an enum, just something that kind of functions like one. Is this possible?

Edit: I'm not really that thrilled about generating a DLL as I've seen in other related problems. I feel like this should be possible with reflection.

Lets suppose I don't need intellisense.

3
  • Reflection and Intellisense don't often go hand-in-hand. Commented Dec 22, 2010 at 4:52
  • @Anthony: Perhaps some trick with anonymous types then? Do you at least understand what I'm trying to do? I just don't want to maintain two lists; one in C# and the other in a DB. Commented Dec 22, 2010 at 4:54
  • Does this answer your question? Automatically create an Enum based on values in a database lookup table? Commented Mar 31, 2020 at 11:58

3 Answers 3

3

You will have to generate an assembly if you want to be able to use the enumeration or class at compilation time. Reflection happens at execution time so that won't give you intellisense.

This is a common problem - there are a set of distinct values in a database table and those values don't change often so they are modeled as an enum in the source code. This allows these somewhat static values to be easily used in a very readable way. The problem is that when the values do change, it would be nice if the enum changed as well.

The problem with trying to keep the enum and database in sync is that an automatic process doesn't change the fact that if you are changing the database it would be very unlikely that you would do so without having to roll new code to leverage the changed value. It is better to model these values as an enum and still store them in the database. Just manually sync them as the need arises.

Sign up to request clarification or add additional context in comments.

7 Comments

Is there a better way to solve the problem of maintaining two lists, one in C# and the other in a DB?
I don't understand the reason to have two lists to begin with, either have it in the DB or C#.
That's not really the answer I wanted to hear, but its an answer none the less. I'm gonna leave my question open in case anybody else has any other answer. I'll mark yours correct if there are no better solutions by tomorrow.
@Phil: I need to have an enum in C# and then a table in the DB that contains the exact contents of the enum. I basically have entities (Animals) that have a type. Depending on the type that is stored in the db, I do something differently in code. Like if (Animal.AnimalType == AnimalType.Fish) {//drawFish()} Here Animal.AnimalType is in the DB and AnimalType.Fish is an enum.
I have seen both used - the database values are useful as foreign keys and the enum is useful to make the application code more readable.
|
3

Try this solution:

using T4 code generation for lookup tables.

1 Comment

Good thing but please write a short description of how it works, the link may will be broken in future.
0

There's always code generation: http://www.mygenerationsoftware.com/ if you don't want to go the reflection route.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.