我的 dbContext return null,当我想获取来自身份的用户列表时

My dbContext return null, when I want get list of user, which are from identity

My dbContext return null,当我想在索引视图中获取用户列表时。此列表来自我的数据库 AspNetUsers table,它是由身份生成的。我可以获取其他我的数据库 table 列表。

有我的ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    public DbSet<ProductCategory> ProductCategories { get; set; }
    public DbSet<ProductBrand> ProductBrands { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
    public DbSet<Address> Address { get; set; }
    public DbSet<Recipe> Recipes { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Order_detail> Order_Details { get; set; }
}

有我的UserController

[Area("Admin")]
public class UserController : Controller
{
    UserManager<IdentityUser> _userManager;
    private ApplicationDbContext _db;
    public UserController(UserManager<IdentityUser> userManager, ApplicationDbContext db)
    {
        _userManager = userManager;
        _db = db;
    }
    public IActionResult Index()
    {
        return View(_db.ApplicationUsers.ToList());
    }
}

有我的ApplicationUser.Model,它继承了IdendityUser

public class ApplicationUser : IdentityUser
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public ICollection<Recipe> Products { get; set; }
    public ICollection<Order> Orders { get; set; }
}

我不知道你是如何在你的 ASP.NET 核心 MVC 应用程序上注册 ApplicationDbContext 和身份框架的,因为你没有在问题上展示它们。

您的代码中有几个问题。


首先,如果您有自定义的 IdentityUser,例如您拥有的 ApplicationUser,则必须使用 IdentityDbContext 的通用版本:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    ...
}

如果您有以下任何一项,则需要使用 IdentityDbContext 的匹配通用版本:

  • 自定义IdentityUser
  • 自定义IdentityRole
  • 自定义主键
  • 所有 7 个 class 用户和角色,加上 IdentityUserRoleIdentityUserClaimIdentityRoleClaimIdentityUserLoginIdentityUserToken

在使用 IdentityDbContext 注册自定义 class 后,您无需将 class 作为 DbSet<> 之一:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    ...

    public DbSet<ProductCategory> ProductCategories { get; set; }
    public DbSet<ProductBrand> ProductBrands { get; set; }
    public DbSet<Product> Products { get; set; }
    // public DbSet<ApplicationUser> ApplicationUsers { get; set; }
    public DbSet<Address> Address { get; set; }
    public DbSet<Recipe> Recipes { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Order_detail> Order_Details { get; set; }
}

您还需要在 Startup.cs 中使用 AddIdentity<TUser>AddDefaultIdentity<TUser>AddIdentityCore<TUser> 的通用版本,具体取决于您的需要:

  • AddDefaultIdentity = AddIdentity + AddDefaultTokens + AddDefaultUI

您没有指定您使用的 ASP.NET Core Identity 版本,所以我不完全知道您使用的是哪个版本,但以下是我注册它的方式:

services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
    options.User.RequireUniqueEmail = ...;

    ...
})
.AddEntityFrameworkStores<ApplicationDbContext>();

我已经自定义了所有 7 个 classes,并将主键从 string 更改为 Guid


最后,要使用注入的依赖项 UserManagerSignInManager,您还需要更正它们的通用版本:

[Area("Admin")]
public class UserController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;

    public UserController(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

    public IActionResult Index()
    {
        // Get the user list
        var users = _userManager.Users.ToList();

        // Build your view model to define what your UI only needs, not just passing
        // everything to it
        var vm = new UserManagementListViewModel
        {
            Users = users.Select(x => new UserViewModel
            {
                UserId = x.Id,
                UserSurname = x.Surname,
                ProductCount = x.Products.Count(),
                OrderCount = x.Orders.Count()
            })
        };
        return View(vm);
    }
}