Base solution for your next web application
Open Closed

Get id of new row #7442


User avatar
0
antonis created

Hi,

I have the following code

Client newClient = await clientRepository.InsertAsync(clientInput);

return ObjectMapper.Map<ClientDto>(newClient);

I am trying to return the new id back to the client. But apparently the Id of the newClient is a huge number -9223372036854775000 which means that the row was inserted but for some reason the Id was not set in the Client object.

How can I overcome this issue?


2 Answer(s)
  • User Avatar
    1
    exlnt created

    Here is an example of how I do it in my app.

         public async Task<long> CreateAddress(CreateAddressInput input)
            {
                var address = ObjectMapper.Map<Address>(input);
                if (AbpSession.TenantId != null)
                {
                    address.TenantId = (int)AbpSession.TenantId;
                }
                //Create address
                var newId = await _addressRepository.InsertAndGetIdAsync(address);
                return newId;
            }
    
  • User Avatar
    0
    maliming created
    Support Team

    Use the InsertAndGetIdAsync method or manually call the SaveChanges method of the current unit of work.