davy
davy

Reputation: 4562

When to use Enums with Entity Framework?

I've added enums to my C# Entity Framework 6, MVC 4 application. I like the approach as:

  1. I think my model is clear that the value comes from a lookup.
  2. I can easily bind them to a select list in my view using approached such as these: examples from Stack Overflow.
  3. I'm not looking up the database to get the values.
  4. I have less plumbing code to use the values.

On the downside, I'm thinking that:

  1. I need to be careful to keep my database in sync with my code for actual lookup tables.
  2. I guess, I would need to deploy the code every time I make a change to one, instead of perhaps having 'administration' tables where a user could add new lookup values. (I guess you could be selective and not use enums for values that would change a lot).

I'm just wondering what the real advantages of this approach are and if there are common scenarios when I would want to avoid it?

Thanks

Upvotes: 2

Views: 356

Answers (1)

James
James

Reputation: 82136

I don't think your question is related to EF at all, it's more backend-agnostic.

The only real problem is what you have already suggested - having to recompile when you need to add/remove values. However, that all really depends on how often you actually expect the list to change.

The point of using an enum is for readability & simplicity from the developers perspective, however, if you enum is really just for UI purposes I'd argue that you could end up giving the developer more work in maintaining the list than doing a lot of work once and not having to worry about it again.

Having to re-compile just for the sake of adding a new option for a user seems very brittle to me.

Upvotes: 1

Related Questions