I have such a task. At the time of registration, the user is added a role. At the moment there are 2. Client, Doctor. Here is the service method that adds the role. everything is clear to me here. And what is most interesting through this service is the role is added to the table that I need, but I can not get to it.

public void AddUserToRole(DtoUser user, string roleName) { var dbUser = repo.GetQuery<AspNetUsers>().SingleOrDefault(e => e.Id == user.Id); if (dbUser == null) return; if (dbUser.AspNetRoles.Count == 0 || dbUser.AspNetRoles.Any(el => el.Name != roleName)) { var role = repo.GetQuery<AspNetRoles>().SingleOrDefault(e => e.Name == roleName); if (role == null) return; dbUser.AspNetRoles.Add(role); repo.SaveChanges(); } } 

Go ahead. The database has a separate table for user roles. But in the model it is not specified anywhere. This table is called "AspNetUsersRoles" here, by Id, the role that is specified during registration is stored (I need it). The rest of the user data falls into a large AspNetUsers table, to which I have access (under access, I mean, it is represented in the model as a class, but there are no roles themselves, because they are in another table) There is a third table, the list of roles themselves "AspNetRoles ". It simply lists the roles in the database. And there is such a diagram. In the first picture, the connection with the table of the listed roles, in the second connection with the main table. Everywhere as I understand they are related to Id. The first link to the table roles

And the second connection with the main table of the user

I need to write a small service or just a Linq request to get their roles out by Id. After all, I can register, but I can’t pull them out for comparison :) Help. Thank.

  • What is repo? - mirypoko 2:07 pm

0