3

I am using Entity Framework Code First to build my database. I have categories which do not change often, but could potentially change in the future. Would it be better practice to store the category data in enums or in an actual table? Below is an example of the ways I would implement the two approaches:

With an enum:

public enum Role
{
    Administrator, Employee, Developer
}

public class User
{
    public int ID {get;set;}
    public string Name { get; set; }
    public Role Role { get; set; }
}

With a separate table:

public class User
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int RoleID { get; set; } // Foreign key to Role table
    public virtual Role Role { get; set; } // Navigation property for Role
}

public class Role
{
    public int ID { get; set; }
    public string RoleName { get; set; }
}

2 Answers2

1

If the data can change use an actual table - I have seen changing enums and it isn't pretty. You will have codes like "first try" "first try second..." - for an example see Microsoft offices interop enums ;)

Christian Sauer
  • 10,351
  • 10
  • 53
  • 85
1

My take on this is to use the second approach (separate table) since it supports separation of concerns (data is separate from implementation). However if you may not be adding more roles any ways then your first approach of using enums should be ok.

sosspyker
  • 83
  • 7