Creating Models that derive Identityuser or use the Aspnetusers table

Asked

Viewed 492 times

3

I am developing management software for my gym.

I have Students, Teachers and system users.

I installed the Identity.

I created a model calling for Aluno who inherits from the IdentityUser

  public class Aluno : IdentityUser
    {

        public int AlunoId { get; set; }
        public string Nome { get; set; }
        //etc
}

Now I was going to create the teachers, so I thought

Professor: IdentityUser

But it would not be better to use the table already created by Identity AspNetUsers?

I am a little confused, because then I will put some roles like "students", "teachers", "admin", "operator", etc., which will be the level of access in the administrative panel of the system.

How would you then add Students. inserir a descrição da imagem aqui But it was a mistake. Object Reference not set to an instance of Object.

  • How to control the pages through Identity? Role X can access page Y Role Z can access page J

1 answer

2


However it would not be better to use the table already created by Identity Aspnetusers?

This form already uses this table. What happens is that the Models bred IdentityUser will all records from the table AspNetUsers.

I am a little confused, because then I will put some roles like "students","teachers","admin","operator", etc that will be the level of access in the administrative panel of the system.

Depending on what the composition of your Model, the filtration by Roll may not be necessary. Roles are interesting when there is no clear distinction between users. As in this case there is a well-modeled distinction, permissioning can be done by checking whether the user exists as that derivation. For example:

// Se usuário é aluno
var userId = User.Identity.GetUserId();
var aluno = db.Alunos.FirstOrDefault(a => a.Id == userId); // retorna um Aluno
var professor = db.Professores.FirstOrDefault(p => p.Id == userId); // retorna null
  • but if they derive, I should insert a student as userManager.Create(student) or db.Alunos.Add(student) ? if it is the 2nd option the id field (vvarchar(max)) q it creates will go blank!

  • UserManager.Create(aluno), hassle-free.

  • I would have to create an Identityconfig, for each one? student, teacher and user? pq the create should have something like this public class Applicationusermanager : Usermanager<Student> public class Applicationusermanager : Usermanager<Teacher>, etc?

  • No need. By inheritance, the UserManager of IdentityUser accepts any derivative entity.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.