我会将数据播种到 AspNet 身份表中。该表已成功创建,但在尝试播种数据时出现错误。
System.InvalidOperationException:尝试激活“Microsoft.AspNetCore.Identity.UserManager”时,“无法解析类型为“Microsoft.Extensions.Logging.ILogger1[Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.IdentityUser]]”的服务`1[Microsoft.AspNetCore.Identity.IdentityUser]
public static void EnsureSeedData(string connectionString)
{
var services = new ServiceCollection();
services.AddDbContext<IdentityDbContext>(options =>
options.UseSqlServer(connectionString));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<IdentityDbContext>()
.AddDefaultTokenProviders();
using (var serviceProvider = services.BuildServiceProvider())
{
using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = scope.ServiceProvider.GetService<IdentityDbContext>();
context.Database.Migrate();
var userMgr = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var alice = userMgr.FindByNameAsync("alice").Result;
if (alice == null)
{
alice = new ApplicationUser
{
UserName = "alice",
Email = "AliceSmith@email.com",
EmailConfirmed = true
};
var result = userMgr.CreateAsync(alice, "My long 123$ password").Result;
if (!result.Succeeded)
{
throw new …Run Code Online (Sandbox Code Playgroud)