微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决我的 dbContext 返回 null,当我想要获取来自身份的用户列表时

当我想在索引视图中获取用户列表时,我的 dbContext 返回 null。此列表来自我的数据库 AspNetUsers 表,该表已通过身份生成。我可以得到其他我的数据库表列表。

这是我的 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 Core MVC 应用程序上注册 ApplicationDbContext 和 Identity 框架,因为您没有在问题中显示它们。

您的代码中有几个问题。


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

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

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

  • 自定义 IdentityUser
  • 自定义 IdentityRole
  • 自定义主键
  • 所有 7 个类、用户和角色,以及 IdentityUserRoleIdentityUserClaimIdentityRoleClaimIdentityUserLoginIdentityUserToken

使用 IdentityDbContext 注册自定义类后,您无需将该类作为 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; }
}

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

  • AddDefaultIdentity = AddIdentity + AddDefaultTokens + AddDefaultUI

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

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

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

我自定义了所有 7 个类,并将主键从 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);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。