E
E
embiid2021-03-24 22:13:04
ASP.NET
embiid, 2021-03-24 22:13:04

How to assign an id to a model?

Explain, please. Here I am writing a service for user registration, everything is fine.
BUT! After that, when I send a request to Postman, it displays to me that the request has been processed. But, the most important thing is that the new user didn't push...
What's wrong? What am I missing?

public async Task<UserDto> Register(UserDto userModel, string password)
        {
            if (!await EmailExists(userModel.Email)) return null;
 
            byte[] passwordHash, passwordSalt;
            HashPassword(password, out passwordHash, out passwordSalt);
 
            var user = _mapper.Map<User>(userModel);
 
            user .PasswordHash = passwordHash;
            user .PasswordSalt = passwordSalt;
 
            var createdUser= await _unitOfWork.UserRepository.AddAsync(user );
 
            return _mapper.Map<userModel>(createdUser);
        }

And of course the controller itself:

public async Task<IActionResult> Register([FromBody] UserRegistrationModel userRegistrationModel)
        {
            var user = _mapper.Map<UserDto>(userRegistrationModel);
 
            await _authService.Register(user , userRegistrationModel.Password);
 
            //return CreatedAtAction(nameof(Login), user );
            return CreatedAtAction(nameof(Login), new { id = user .Id }, userRegistrationModel);
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2021-03-25
@yarosroman

public async Task<IActionResult> Register([FromBody] UserRegistrationModel userRegistrationModel)
        {
            var user = _mapper.Map<UserDto>(userRegistrationModel);
 
            var newUser = await _authService.Register(user , userRegistrationModel.Password);
 
            //return CreatedAtAction(nameof(Login), user );
            return CreatedAtAction(nameof(Login), new { id = newUser .Id }, userRegistrationModel);
        }

Maybe it's necessary?

L
lolik20, 2021-03-27
@lolik20

Set the ID field to the sql IDENTITY parameter. And do not assign id manually

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question