![](/img/trans.png)
[英]Why can it be that my ApiKey variable is in null when I try to create a new user using ASP.NET Core Identity?
[英]My dbContext return null, when I want get list of user, which are from identity
当我想在索引视图中获取用户列表时,我的 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; }
}
有我的用户控制器
[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
IdentityUserRole
、 IdentityUserClaim
、 IdentityRoleClaim
、 IdentityUserLogin
和IdentityUserToken
使用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 核心标识,所以我不完全知道您使用的是哪个版本,但以下是我注册它的方式:
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.User.RequireUniqueEmail = ...;
...
})
.AddEntityFrameworkStores<ApplicationDbContext>();
我自定义了所有 7 个类,并将主键从string
更改为Guid
。
最后,要使用依赖注入的UserManager
和SignInManager
,您还需要更正它们的通用版本:
[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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.