✅ Best practice with EF Core "constants"?
How to work with user groups correctly: I have a User relation that contains the id of the group in which it is located. By default, there are two user roles: User with id 1 and Admin with id 2. There are services that need to know which group a user of these two is in. How to properly organize it? Create a constant with the id of the user role and the id of the admin role and pass it throughout the application? Or create a constant with the name of the role, and each time look for the ID of this role by name? Or are there better ways to deal with this?
2 Replies
of you're going to hard-code references to entities, reference them by PK
actually, forget the "hard-code"
always reference entities by PK
on top of that, there's probably a better way to deal with this
permissions-based auth is generally much preferred to role-based
in that scenario, all of your permissions are basically static data in your schema, and EF has a mechanism for defining static data explicitly
in this case, you hard code, say, one or more enums with all your perms, and then a little bit of EF code that derives the static data entities FROM that
if you want to just do that same thing with roles, that's not terrible, it's just going to be less extensible
Thanks a lot! Good explanation!