C# 为什么不';我的标识符和应用程序用户是否正在填充到我的数据库中?

C# 为什么不';我的标识符和应用程序用户是否正在填充到我的数据库中?,c#,asp.net,asp.net-core-mvc,asp.net-core-3.1,C#,Asp.net,Asp.net Core Mvc,Asp.net Core 3.1,在我的.net core 3.1 web应用程序中植入用户时遇到问题。正在SQL Server上创建相应的表,但在运行应用程序时未创建任何行。我不知道为什么数据库没有被填充。有人能发现这个问题吗 DBInitializer文件,用于将用户种子植入数据库中 public class DBInitializer { public static async Task Initialize(IServiceProvider services) { ApplicationD

在我的.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();
        }
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)
    {

    }


}
public类ApplicationDbContext:IdentityDbContext
{
公共数据库集应用程序用户{get;set;}
公共DbSet AppUserAs{get;set;}
公共DbSet AppUserBs{get;set;}
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
}

以下是一个工作演示:

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)
    {
    }
}
公共类应用程序用户:IdentityUser
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串FullName=>FirstName+“”+LastName;
公共应用程序用户()
{
}
公共应用程序用户(字符串名、字符串名、字符串电子邮件、字符串电话号码、字符串密码)
{
this.FirstName=FirstName;
this.LastName=LastName;
this.Email=电子邮件;
this.PhoneNumber=电话号码;
this.UserName=电子邮件;

PasswordHasher

不确定这是否有帮助,但EF Core在EF Core 2.1中获得了a。您需要调用
DBInitializer.Initialize
方法,但我在您的示例代码中看不到它。Hi@WaiterAmstrong,我的回答是否帮助您解决了问题?如果是,您可以吗?
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)
    {

    }


}
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)
    {
    }
}
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>();
            });
}
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();
}