public async Task <Guid> CreateAsync(UserForCreationDto creationDto) { UserEntity newUser = Activator.CreateInstance <UserEntity>(); newUser.IsActive = true; foreach (PropertyInfo propertyInfo in creationDto.GetType().GetProperties()) { if (newUser.GetType().GetProperty(propertyInfo.Name) != null) { newUser.GetType().GetProperty(propertyInfo.Name).SetValue(newUser, propertyInfo.GetValue(creationDto, null)); } } newUser.UserName = creationDto.Email; await _userManager.CreateAsync(newUser, creationDto.Password); foreach (string roleName in creationDto.RoleNames) { await _userManager.AddToRoleAsync(newUser, roleName); } await _userManager.UpdateAsync(newUser); return(newUser.Id); }
public async Task <Guid> CreateAsync(UserForCreationDto creationDto) { UserEntity newUser = Activator.CreateInstance <UserEntity>(); newUser.IsActive = true; foreach (PropertyInfo propertyInfo in creationDto.GetType().GetProperties()) { if (newUser.GetType().GetProperty(propertyInfo.Name) != null) { newUser.GetType().GetProperty(propertyInfo.Name).SetValue(newUser, propertyInfo.GetValue(creationDto, null)); } } newUser.UserName = creationDto.UserName; await _userManager.CreateAsync(newUser, creationDto.Password); foreach (string roleName in creationDto.RoleNames) { await _userManager.AddToRoleAsync(newUser, roleName); } // await _userManager.UpdateAsync(newUser); foreach (Guid storageId in creationDto.StorageIds) { var storage = await _context.Storages.SingleOrDefaultAsync(s => s.Id == storageId); if (storage == null) { throw new Exception("Can not find storage with Id"); } var userStorage = new UserStorageEntity() { UserId = newUser.Id, StorageId = storageId }; await _context.UserStorages.AddAsync(userStorage); await _context.SaveChangesAsync(); } return(newUser.Id); }