I am developing CRUD API with my data layer implementing generic repository and unit of work pattern. I have a customer model which is a complex model.
The create endpoint of my api works fine(it creates customer along with related entities) but the update operation does not update the related entities even though they are sent to the update endpoint correctly.
I cannot figure what I am doing wrongl so your assistance is greatly appreciated.
Please find attached the relevant portions of my code.
Customer model
<pre>public class CustomerForNewDTO
{
[Required] public string CustSupCode { get; set; } = string.Empty;
[Required] public string CompanyName { get; set; } = string.Empty;
public decimal Balance { get; set; }
public List<AddressForNewDto> Addresses { get; set; } = new List<AddressForNewDto>();
public List<ContactForNewDto> Contacts { get; set; } = new List<ContactForNewDto>();
}
Update endpoint
[HttpPut("updatecust")]
public async Task<ActionResult<bool>> UpdateCustomer(CustomerForNewDTO cu)
{
if (cu == null)
{
return BadRequest();
}
var cus = _mapper.Map<Customer>(cu);
_unitOfWork.Repository<Customer>().Update(cus);
if (await _unitOfWork.CompleteAsync() > 0)
{
return Ok(true);
}
return BadRequest();
}
Generic Repository
public void Update(TEntity entity)
{
_context. Set<TEntity>().Attach(entity);
_context.Entry(entity).State = EntityState.Modified;
}
Thank you in advance for your assistance
What I have tried:
I have tried google search for solution but to no avail