使用Identity Framework在迁移Up()中创建用户

时间:2017-08-01 09:30:26

标签: asp.net-mvc entity-framework asp.net-identity

我添加了一个迁移来创建用户,但代码在点击userRepo.Create(...)时会挂起,并且在此方法中_userManager.Create(...)

using (UserRepository userRepo = new UserRepository())
{
    User adminUser = new User() { IsActive = true, UserName = 
    "admin@testing.com", CompanyId = 1, Password = 
    "admintesting" };
    adminUser.Role = new Models.Security.Role() { Id = 2 };
    userRepo.Create(adminUser);
}

Create方法如下

public IdentityResult Create(Model.User user)
{
   var userEntity = Mapper.Map<Entity.Security.User>(user);
   _dbContext.Set<Entity.Security.User>().Add(userEntity);
   var result = _userManager.Create(userEntity, userEntity.Password);
   DetachAllEntities();
   return result;
}

_dbContext继承自IdentityDbContext并相应地实例化

UserManager<Entity.Security.User, int> _userManager = new UserManager<Entity.Security.User, int>(new UserStore<Entity.Security.User, Entity.Security.Role, int, Entity.Security.UserLogin, Entity.Security.UserRole, Entity.Security.UserClaim>(_dbContext));

等效的异步方法在应用程序的其他地方工作,但我希望迁移的非异步方法。任何帮助都非常感谢。

0 个答案:

没有答案