简体   繁体   English

如何在dotnet核心中将ApplicationUser添加为测试数据

[英]How to add ApplicationUser as test data in dotnet core

I'm trying to initialize some test data in my database, and having some problems adding password to the ApplicationUser in Identity Framework. 我正在尝试初始化数据库中的一些测试数据,并且在向Identity Framework中的ApplicationUser添加密码时遇到一些问题。

When I have done this earlier, I have used the seed method like this: 早先完成此操作后,就使用了如下的seed方法:

protected override void Seed(ApplicationDbContext db)
{
    var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

    var adminuser = new ApplicationUser { Email = "admin@test.no", UserName = "admin@test.no", FirstName = "admin", LastName = "test" };
    userManager.Create(adminuser, "Password1.");
    userManager.AddToRole(adminuser.Id, role:"admin");
}

but as the seed method is not supported in dotnet core I have tried to add the dummy data in the following way: 但是由于dotnet核心不支持种子方法,因此我尝试通过以下方式添加虚拟数据:

using (var serviceScope = 
app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
    db.Database.EnsureCreated();

    db.AddRange(
        new ApplicationUser { UserName = "ola@nordmann.no", Email = "ola@nordmann.no" },
        new ApplicationUser { UserName = "test@test.no", Email = "test@test.no" }
    );
    db.SaveChanges();

I have also tried to use the same method I used in the seed method, but that doesn't work either as I get the following error message on the line were I add result1 to the database: 我也尝试过使用与seed方法中使用的方法相同的方法,但是由于我将result1添加到数据库中时出现以下错误消息,因此该方法也不起作用:

cannot convert from 'System.Threading.Tasks.Task' to 'Server.Models.ApplicationUser 无法从“ System.Threading.Tasks.Task”转换为“ Server.Models.ApplicationUser”

using (var serviceScope = 
app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
    var db = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
    var userManager = app.ApplicationServices.GetService<UserManager<ApplicationUser>>();

    var au1 = new ApplicationUser { UserName = "test@test.no", Email = "test@test.no" };
    var result1 = userManager.CreateAsync(au1, "Test123");
    db.Users.Add(result1);

You can use PasswordHasher in combination with user. 您可以将PasswordHasher与用户结合使用。

ie

var user = new User(); 
var hasher = new PasswordHasher<User>();

db.AddRange(
        new ApplicationUser { UserName = "ola@nordmann.no", Email = "ola@nordmann.no", PasswordHash = hasher.HashPassword(user, "Password1") },

    );
    db.SaveChanges();

If anyone else should be interested, I ended up with the following solution: 如果其他人应该对此感兴趣,那么我将得出以下解决方案:

public async Task CreateUsersAndRoles(IServiceScope serviceScope)
{
    var userManager = serviceScope.ServiceProvider.GetService<UserManager<ApplicationUser>>();

    await userManager.CreateAsync(new ApplicationUser { UserName = "ola@nordmann.no", Email = "ola@nordmann.no"}, "Password1.");
}

and call on this method within the Configure method in the startup file like this: 并在启动文件的Configure方法中调用此方法,如下所示:

await CreateUsersAndRoles(app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM