2

I am currently in the early stages of creating a MVC application. I am creating the database using code-first and am wondering what the recommended protocol is to store constant values. To give some context, I have a model class called Appointments which is mapped to a SQL Table. An Appointment can be in one of many states e.g. 'Ready', 'Waiting', 'Cancelled' etc. Normally I would create a Statuses table in SQL and have a foreign key between the Appointment and Statuses table and have a enumeration in my code that I use to assign Statuses i.e.

Appointment app = new Appointment()
{
    StatusID = (int)ExternalClass.Statuses.Waiting
};

Is this the recommended best practice? My only concern is that I am relying on the id's of the rows in SQL matching up to the enumeration and not sure if this will always be the case. Any suggestions of better methods would be appreciated.

tereško
  • 58,060
  • 25
  • 98
  • 150
chead23
  • 1,859
  • 11
  • 12
  • 2
    ef5 supports enums. are you using it? – Daniel A. White Feb 21 '13 at 20:36
  • No I did not know that. I will investigate now. – chead23 Feb 21 '13 at 20:37
  • My understanding is that enums are supported, but only if you're using .NET 4.5 along with EF5 (I'm looking forward to it). – John Saunders Feb 21 '13 at 20:38
  • I'm currently using EF 5.0 with .NET 4.0. – chead23 Feb 21 '13 at 20:40
  • 1
    [This article](http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx) describes an approach for faking Enums with EF that I've used successfully, and from [this answer](http://stackoverflow.com/questions/6344032/enums-with-ef-code-first-standard-method-to-seeding-db-and-then-using) it looks like others have too. – Brian S Feb 21 '13 at 20:55

1 Answers1

1

Assuming you're not updating/inserting/deleting records in the "states" table while the application is deployed, this methodology works perfectly fine, and provides beautifully softcoded code with syntactic sugar.

Brad M
  • 7,857
  • 1
  • 23
  • 40