3

我正在为我的用户管理系统使用 ASP.NET Core 2.2 Identity。我需要有几种类型的用户......例如,仓库用户和应用程序用户,我创建了一个从身份用户类继承的基类 =>IdentityUser<long>

public class BaseApplicationUser : IdentityUser<long>
{
    public string FirstName { set; get; }
    public string LastName { set; get; }
    ...
}

仓库用户和商店用户继承自BaseApplicationUser创建不同的用户。

public class ApplicationUser : BaseApplicationUser
{
}

我只想为所有这些人准备一张桌子=>AspNetUsers 添加OnModelCreating此代码:

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<ApplicationUser>();
    builder.Entity<WarehouseApplicationUser>();
}

并在 Context 中的 DbSets 中:

public DbSet<ApplicationRole> ApplicationRole { get; set; }
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<WarehouseApplicationRole> WarehouseApplicationRole { get; set; }
public DbSet<WarehouseApplicationUser> WarehouseApplicationUsers { get; set; }

我需要为用户创建单独的角色类,比如ApplicationRoleWarehouseApplicationUsers.

有什么问题?

  1. 如何在 asp.net core Identity 中创建不同的用户?( 最好的办法 )
  2. 另一个问题是,当我向 中添加一个字段时ApplicationUser,该字段已成功添加到 中AspNetUsers,但是当我向 中添加一个字段时WarehouseApplicationUsers,EF Core 添加了名为WarehouseApplicationUsersand的新表WarehouseApplicationRoleWarehouseApplicationUserRole然后向WarehouseApplicationUsers表中添加了一个字段……这是什么!

最后,如何继承和构建不同用户的层次结构?非常感谢

4

2 回答 2

2

问题1: How to create different users in asp.net core Identity ?

ASP.NET Core 提供了一个内置方法:AddIdentityCore<TUser>.

你可以像这样使用它:services.AddIdentityCore<ApplicationUser>();

更多细节你可以看到这个线程

问题2:

这是一个示例,您可以与您的代码进行比较:

基础应用用户:

 public class BaseApplicationUser: IdentityUser
{
    public string FirstName { set; get; }
    public string LastName { set; get; }
}

应用用户:

 public class ApplicationUser : BaseApplicationUser
{
    public string Year { get; set; }
}

仓库应用用户:

public class WarehouseApplicationUser : BaseApplicationUser
{
     public string Age { get; set; }
}

应用程序上下文:

public class ApplicationDbContext : IdentityDbContext<BaseApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
  
    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
   
    public DbSet<WarehouseApplicationUser> WarehouseApplicationUsers { get; set; }
}

迁移结果: 在此处输入图像描述

于 2020-11-02T07:43:26.843 回答
-1

NetCore 6.0.1和 EfCore 6.0.1:

数据库上下文:

using Attendance.Server.Data.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Attendance.Server.Data;
// this line in important :
public class AppDbContext : IdentityDbContext<User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

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

        // AspNet Identity
        modelBuilder.Entity<User>(entity => { entity.ToTable("User"); });
        modelBuilder.Entity<Role>(entity => { entity.ToTable("Role"); });
        modelBuilder.Entity<UserRole>(entity => { entity.ToTable("UserRole"); });
        modelBuilder.Entity<RoleClaim>(entity => { entity.ToTable("RoleClaim"); });
        modelBuilder.Entity<UserClaim>(entity => { entity.ToTable("UserClaim"); });
        modelBuilder.Entity<UserLogin>(entity => { entity.ToTable("UserLogin"); });
        modelBuilder.Entity<UserToken>(entity => { entity.ToTable("UserToken"); });
    }
}

像这样创建所有模型

using Microsoft.AspNetCore.Identity;
namespace Attendance.Server.Data.Entities;
public class User : IdentityUser<int> { }

不要忘记在您的 Web 项目中安装这些软件包:

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Design

在您的program.cs中添加这一行:

builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

你的appsettings.json

"Logging": {
"LogLevel": {
  "Default": "Information",
  "Microsoft.AspNetCore": "Warning"
  }
},
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=.\\SQLEXPRESS;Initial Catalog=Attendance;Integrated Security=True;MultipleActiveResultSets=True"
  }

最后在 Visual Studio 中右键单击您的项目(不是解决方案)并选择Open in Terminal,然后键入这些行以迁移和创建数据库:

dotnet ef migrations add createDB -o data/migrations
dotnet ef database update
于 2021-12-26T14:34:32.770 回答