如何正确继承ASP.net Core 2.1中的ApplicationUser用户

时间:2019-02-21 23:48:06

标签: asp.net asp.net-mvc

我想做的是通过从ApplicationUser继承为不同的用户创建不同的注册页面,以便我可以记录其他属性,例如Customer和Employee。我没有保存值,也不确定是我的数据模型还是RegisterEmployee.cs类。

RegisterEmployee.cs

 public async Task<IActionResult> OnPostAsync(string returnUrl = null)
 {
        if (ModelState.IsValid)
        {
            var user = new EmployeeUser
            {
                UserName = Input.Email,
                Email = Input.Email,                   
                DriversLicense = Input.DriversLicense,
                DOB = Input.DOB
            };
       }
}

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
    public virtual Employee Employee{ get; set; }
    public virtual Customer Customer { get; set; }

    public ICollection<ApplicationUserRole> UserRoles { get; set; }
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    public string Image { get; set; }

}

public class ApplicationUserRole : IdentityUserRole<string>
{
    public virtual ApplicationUser User { get; set; }
    public virtual ApplicationRole Role { get; set; }
}

public class ApplicationRole : IdentityRole
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

Employee.cs

public class Employee
{
    [Required]
    [Display(Name = "SSN or TaxID")]
    public string SsnTaxId { get; set; }
    [Display(Name = "Date of Birth")]
    public DateTime DOB { get; set; }
    [Required]
    [Display(Name = "License Class")]
    public char DriverClass { get; set; }
    [Required]
    [Display(Name = "DL Number")]
    public string LicenseNumber { get; set; }
}

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>,
ApplicationUserRole, IdentityUserLogin<string>,
IdentityRoleClaim<string>, IdentityUserToken<string>>

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

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<ApplicationUserRole>(userRole =>
        {
            userRole.HasKey(ur => new { ur.UserId, ur.RoleId });

            userRole.HasOne(ur => ur.Role)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.RoleId)
                .IsRequired();

            userRole.HasOne(ur => ur.User)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.UserId)
                .IsRequired();
        });
    }

    public DbSet<ApplicationUser> ApplicationUser { get; set; }
    public DbSet<Employee> Employee{ get; set; }
    public DbSet<Customer> Customer{ get; set; }
}

0 个答案:

没有答案