Search code examples
c#asp.netrazor-pagesidentity

ASP.NET Razor Pages Identity


I'm almost new to ASP.NET and I have a question about it.

I created a project - Web app with Razor Pages, I made dependency injection and also made some migrations. I have some classes like Student, Teacher, and Admin. I'm using Identity and code-first.

My problem is: I have many users but for my DB it's only one table with all those users. I give them some roles, in one table I have UserId and RoleId, which means they have connections.

Users table:

CREATE TABLE [dbo].[AspNetUsers] (
[Id]                            NVARCHAR (450)     NOT NULL,
[UserName]                      NVARCHAR (256)     NULL,
[NormalizedUserName]            NVARCHAR (256)     NULL,
[Email]                         NVARCHAR (256)     NULL,
[NormalizedEmail]               NVARCHAR (256)     NULL,
[EmailConfirmed]                BIT                NOT NULL,
[PasswordHash]                  NVARCHAR (MAX)     NOT NULL,
[SecurityStamp]                 NVARCHAR (MAX)     NULL,
[ConcurrencyStamp]              NVARCHAR (MAX)     NULL,
[PhoneNumber]                   NVARCHAR (MAX)     NULL,
[PhoneNumberConfirmed]          BIT                NOT NULL,
[TwoFactorEnabled]              BIT                NOT NULL,
[LockoutEnd]                    DATETIMEOFFSET (7) NULL,
[LockoutEnabled]                BIT                NOT NULL,
[AccessFailedCount]             INT                NOT NULL,
[AreaCodeAndTown]               NVARCHAR (100)     NULL,
[Discriminator]                 NVARCHAR (MAX)     DEFAULT (N'') NOT NULL,
[Name]                          NVARCHAR (50)      NULL,
[RoadNameAndNumber]             NVARCHAR (100)     NULL,
[StudentUser_Name]              NVARCHAR (50)      NULL,
[TeacherUser_AreaCodeAndTown]   NVARCHAR (100)     NULL,
[TeacherUser_Name]              NVARCHAR (50)      NULL,
[TeacherUser_RoadNameAndNumber] NVARCHAR (100)     NULL,
CONSTRAINT [PK_AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)

);

Student class:

public class StudentUser : IdentityUser
{
    [Required]
    [MaxLength(50)]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [NotMapped] //ignore
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Compare(nameof(Password))]
    [NotMapped] //ignore
    public string ConfirmPassword { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
} 

Teacher class:

public class TeacherUser : IdentityUser
{
    [Required]
    [MaxLength(50)]
    public string Name { get; set; }
    
    [MaxLength(100)]
    public string RoadNameAndNumber { get; set; }
    
    [MaxLength(100)]
    public string AreaCodeAndTown { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [NotMapped] //ignore
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Compare(nameof(Password))]
    [NotMapped] //ignore
    public string ConfirmPassword { get; set; }
}

ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext
{

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {}

    public DbSet<AdminUser> AdminUsers { get; set; }
    public DbSet<StudentUser> StudentUsers { get; set; }
    public DbSet<TeacherUser> TeacherUsers { get; set; }
    public DbSet<Course> Courses { get; set; }
    public DbSet<Test> Tests { get; set; }
}

Question: How I can get exactly Teacher or Student from this table "Users"? I searched in google, but almost all information about MVC, not about Razor Pages.

Thank you in advance!


Solution

  • I found my mistake.

    I have to change my Discriminator, cause there was only IdentityUser, but I tried to call StudentUser or TeacherUser.

    Now everything is perfect. I hope this info can help someone.