Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

It depends on how you set up the relationships in the configuration of EF Core. There are a lot of videos and blog articles, and tutorial websites that cover this. Here is a good place to start: ef core tutorial - Google Search[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900