如何在静态类中引用ASP.NET标识Usermanager?

时间:2018-04-23 21:28:35

标签: asp.net-core dependency-injection asp.net-identity

我想在程序的种子方法中创建一个新用户但是如何调用CreateAsync?

 public class DbInitializer
{
    private readonly UserManager<ApplicationUser> _userManager;

    public DbInitializer(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

    public static void Initialize(IApplicationBuilder app)
    {
        OneDbContext context = app.ApplicationServices.GetRequiredService<OneDbContext>();
        context.Database.EnsureCreated();

        //If there are no users, create a test user
        if (!context.Users.Any())
        {
            var user = new ApplicationUser { UserName = "test", Email = "test@test.com" };
            var result =  _userManager.CreateAsync(user, "test");
        }

错误CS0120:非静态字段,方法或属性需要对象引用&#39; DbInitializer._userManager&#39;

0 个答案:

没有答案