1

As a developer I believe in empowering my users. One simple way of doing this is allowing certain users ability to alter Look-up Tables. I have always developed on a database first model, but I am starting a new project that I'm going to try code first.

I know you can enable enums on your entities in EF 5+. However, from what I have read all of the enums are assigned in the code. My question is what if I have a Enum GenericTemperature Cold, 1 Hot, 2 Room Temp, 3

Then 3 months go by and the users want to add

Warm, 4 Cool, 5

In database first method I use a solution similar to https://stackoverflow.com/a/2636204/2120857 to handle my enum updates and it works wonderful. However, that seems a little silly for what I would consider a code first project.

On top of that, I typically track all edits on database and have fields CreatedBy, CreatedDate, ModifiedBy, ModifiedDate and (datetime)Inactive to track changes and possible inactivity of types.

  1. Is it possible to give the users the ability to change enums?

  2. Is there something I'm missing or is this capability not in EF Codefirst and I need to use my t4 solution?

Community
  • 1
  • 1
EZE
  • 69
  • 10
  • 1
    I would recommend using enums only if you expect that changing the enum values would also require a new release of your application (e.g. the enum values are tied to business logic). If the values are pretty much cosmetic (at least as far as the code is concerned), and could reasonably change during a single release, then you probably want to use a separate entity instead of an enum. – p.s.w.g Mar 19 '15 at 18:11
  • That's what I assumed the answer would be. I see using Enums for a StatusType as I would have to create new logic for a new status "presumably," but Data Tracking Types for reporting wouldn't necessarily require code changes. – EZE Mar 19 '15 at 18:16

2 Answers2

0

Enums are really just a well-defined means of allowing a .NET developer to specify the behavior of a method or class that they want to call. They aren't there to serve as an unbounded collection which gets populated by the end-users of a database.

  • 2
    I would tend to disagree with the first sentence, enums have many purposes, and while broadly speaking it "specifies behavior" it can also just be a restricted-range of values. The second statement is correct, they aren't supposed to be dynamic. – BradleyDotNET Mar 19 '15 at 18:51
  • While I am stating the functional purpose of enums in a very generic manner as you say, I would suggest that this style improves clarity when also identifying the actors involved in its use--both authoring and consuming--which provides an answer to the question with brevity. – puromtec Mar 19 '15 at 21:19
  • I'm saying that they aren't *just* for that. For example, the `Visibility` enum defines the three states a control can have (in terms of visibility). It doesn't change the behavior of any methods/classes (though you could argue it changes the behavior of the "set" half of the `Visible` property). Sometimes they really are just data. I appreciate your response though :) – BradleyDotNET Mar 19 '15 at 21:22
0

You should use a Dictionary (System.Collections.Generic.Dictionary), it's really easy to add and get data with that type of collection.

You can persist it in a file with serialization or in a database.

Gabriel
  • 333
  • 2
  • 8