gpt4 book ai didi

c# - 为什么我的 IdentityRoles 和 ApplicationUsers 没有填充到我的数据库中?

转载 作者:行者123 更新时间:2023-12-03 20:56:50 26 4
gpt4 key购买 nike

我在我的 .net core 3.1 Web 应用程序中播种用户时遇到问题。正在 SQL Server 上创建相应的表,但在我运行我的应用程序时没有创建任何行。我不知道为什么没有填充数据库。任何人都可以发现问题吗?

DBInitializer 文件将用户种子到数据库

public class DBInitializer
{
public static async Task Initialize(IServiceProvider services)
{
ApplicationDbContext database = services.GetRequiredService<ApplicationDbContext>();

UserManager<ApplicationUser> userManager = services.GetRequiredService<UserManager<ApplicationUser>>();

RoleManager<IdentityRole> roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();

string roleA = "A";
string roleB = "B";

if (!database.Roles.Any())
{
IdentityRole role = new IdentityRole(roleA);
await roleManager.CreateAsync(role);
IdentityRole roleOne = new IdentityRole(roleB);
await roleManager.CreateAsync(roleOne);
await database.SaveChangesAsync();
}

if (!database.ApplicationUsers.Any())
{

AppUserA appUser = new AppUserA("App", "UserA", "AppUserA1@wmu.edu",
"306.000.0001", "AppUserA1");
await userManager.CreateAsync(appUser);
await userManager.AddToRoleAsync(appUser, roleA);

AppUserB appUserFour = new AppUserB("App", "UserB1", "AppUserB1@wmu.edu",
"306.000.0001", "AppUserB1");
await userManager.CreateAsync(appUserFour);
await userManager.AddToRoleAsync(appUserFour, roleA);
await database.SaveChangesAsync();
}

扩展Asp.net IdentityUser 类的ApplicationUser 类
public class ApplicationUser : IdentityUser
{
public String FirstName { get; set; }
public String LastName { get; set; }

public String FullName => FirstName + " " + LastName;

public ApplicationUser()
{
}

public ApplicationUser(string firstName, string lastName, string email, string phoneNumber, string password)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Email = email;
this.PhoneNumber = phoneNumber;
this.UserName = email;
PasswordHasher<ApplicationUser> passwordHasher = new PasswordHasher<ApplicationUser>();
this.PasswordHash = passwordHasher.HashPassword(this, password);

this.SecurityStamp = Guid.NewGuid().ToString();
}

AppUserA 扩展应用程序用户的类
 public class AppUserA : ApplicationUser
{
public AppUserA()
{
}

public AppUserA(string firstName, string lastName, string email, string phoneNumber, string password)
: base(firstName, lastName, email, phoneNumber, password)
{
}
}

ApplicationDBContext 文件:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

public DbSet<ApplicationUser> ApplicationUsers { get; set; }

public DbSet<AppUserA> AppUserAs { get; set; }

public DbSet<AppUserB> AppUserBs { get; set; }


public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{

}


}

最佳答案

这是一个如下所示的工作演示:

1.型号:

public class ApplicationUser : IdentityUser
{
public String FirstName { get; set; }
public String LastName { get; set; }

public String FullName => FirstName + " " + LastName;

public ApplicationUser()
{
}

public ApplicationUser(string firstName, string lastName, string email, string phoneNumber, string password)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Email = email;
this.PhoneNumber = phoneNumber;
this.UserName = email;
PasswordHasher<ApplicationUser> passwordHasher = new PasswordHasher<ApplicationUser>();
this.PasswordHash = passwordHasher.HashPassword(this, password);

this.SecurityStamp = Guid.NewGuid().ToString();
}
}
public class AppUserA : ApplicationUser
{
public AppUserA()
{
}

public AppUserA(string firstName, string lastName, string email, string phoneNumber, string password)
: base(firstName, lastName, email, phoneNumber, password)
{
}
}
public class AppUserB : ApplicationUser
{
public AppUserB()
{
}

public AppUserB(string firstName, string lastName, string email, string phoneNumber, string password)
: base(firstName, lastName, email, phoneNumber, password)
{
}
}

2.程序.cs:
public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
CreateDbIfNotExists(host);
host.Run();
//CreateHostBuilder(args).Build().Run();
}
private static async Task CreateDbIfNotExists(IHost host)
{
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;

try
{
await DBInitializer.Initialize(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred creating the DB.");
}
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}

3.Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<ApplicationUser,IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

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

结果:
enter image description here

关于c# - 为什么我的 IdentityRoles 和 ApplicationUsers 没有填充到我的数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60581580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com