Reputation: 893
I have a database with a table called 'Roles', with 'Id' and 'RoleName' columns. These are the rows in the table:
A user in the 'User' table can have several roles to it. I would like to have LINQ code to check if a specific user has the role 'Technician'.
This is my code for now :
using (MyDbEntities entities = new MyDbEntities())
{
User user = entities.Users.Where(u => u.Id == userIdToFetch).Single();
// Check if user is 'Technician'
if (user.Roles.Any(role => role.Name == "Technician") == true)
{
// user IS technician - do work here
}
}
My problem is - I don't want to hard-code the string "Technician" to my code, because maybe one day the name of the role in the database will change to "Tech".
I have looked at using 'Resources', but that is no good, because changing a string in a resource means recompiling the project.
I do not want to add a section to the configuration file that holds this information, because it seems to me as over-kill...
Any suggestions ?
Upvotes: 0
Views: 608
Reputation: 1969
Either you can do this by putting a XML file of Roles in your solution or add the enteries in .config file.
Upvotes: 0
Reputation: 19111
I would think either a configuration file or Resources is exactly what you would want to use. I guess it's just a question of finding the correct balance:
Either avoid the complexity of the configuration-file, but require compilation, or avoid compilation, but find somewhere else (a config-file of some sort, whether it is a text-file, or a .dll with constants that you can read, or something else) to store it in. In any case, you will need to create something to read it.
Upvotes: 0
Reputation: 20320
You could put it in as a setting, which means you could them amend the config file, though that would be on a per user basis.
You could create a dll to hold the strings, and then refernce it, then an update would only mean redeploying the dll
You could put the strings in file, and load them out of there, bearing in mind you'd have to deploy the file as well.
Upvotes: 2
Reputation: 776
I'd use a table where I store all the different roles and associate the ID of the role with the role ID of the user. That way you can change the name of the role as much as you like as long as the role ID stays the same.
Upvotes: 1