Base solution for your next web application
Open Closed

Getting newly created user's ID #2609


User avatar
0
parsyegl created

I have a problem retrieving the ID of a newly created user in the CreateUser task of the UserAppService. It seems that the new user in the database is not created before the CreateUser task was left. Any ideas on how to get the ID in order to do some further processing?

public async Task CreateUser(CreateUserInput input)
        {
            var user = input.MapTo<User>();

            user.UserName = user.UserName.Trim();
            user.EmailAddress = user.EmailAddress.Trim();
            user.TenantId = AbpSession.TenantId;
            user.Password = new PasswordHasher().HashPassword(input.Password);
            user.IsEmailConfirmed = false;
            user.SetNewEmailConfirmationCode();

            CheckErrors(await UserManager.CreateAsync(user));
            // user.Id is still 0 here

            var newUser = UserManager.FindByEmail(user.EmailAddress.Trim());
            // newUser == null

            //User is not created in DB before CreateUser task is left
        }

1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can use CurrentUnitOfWork.SaveChanges(); to get user's id.