.NET 查询 Aspnetusers 而不是自定义 AuthUser,但 AspNetUser 不存在

.NET querying Aspnetusers instead of Custom AuthUser and Yet AspNetUser does not Exist

我写了一个自定义的 IdentityUser,它是 AuthUser。

public class AuthUser : IdentityUser
    {
        public int StudentsId { get; set; }
        public virtual Students StudentProfile { get; set; }

        public int InstructorId { get; set; }
        public virtual Instructor InstructorProfile { get; set; }

        public bool IsStudent { get; set; }
        public bool IsInstructor { get; set; }
    }

如您所见,上下文没问题

 public class LmsContext : IdentityDbContext<AuthUser> //DbContext
    {
}

startup.cs设置完毕

services.AddDbContext<LmsContext>(options =>
                options.UseSqlServer(
                Configuration.GetConnectionString("UCIPrimarySchool"))
                );
            services.AddDefaultIdentity<AuthUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<LmsContext>();

            services.AddControllersWithViews();
            services.AddRazorPages();

但是当我尝试登录时出现以下错误。

An unhandled exception occurred while processing the request.
SqlException: Invalid object name 'AspNetUsers'.

Microsoft.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__169_0(Task<SqlDataReader> result)

为什么不查询扩展的 AuthUser 而是查询 none 现有的 table AspNetUsers?

首先要理清关系,然后正确迁移和更新数据库。

像这样更改您的 AuthUser:

public class AuthUser : IdentityUser
{
    public virtual Students StudentProfile { get; set; }

    public virtual Instructor InstructorProfile { get; set; }

    public bool IsStudent { get; set; }
    public bool IsInstructor { get; set; }
}

在你的Context中:

  public DbSet<Students>  Students { get; set; }
  public DbSet<Instructor> Instructor { get; set; }

迁移和更新:

成功更新数据库后,您需要更改您的View/Shared/_LoginPartial代码:

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

@inject SignInManager<AuthUser> SignInManager
@inject UserManager<AuthUser> UserManager

然后

Select your LmsContext 添加

然后启动您的应用程序并登录。