I have the following method in my controller:
[HttpPost]
public async Task<ActionResult<ChartsCategoryDto>> PostChartsCategory(ChartsCategoryDto chartsCategory)
{
try
{
var category = _mapper.Map<ChartsCategoryDto, ChartsCategory>(chartsCategory);
if (category.Id == 0)
{
category = _context.ChartsCategories.Add(category).Entity;
await _context.SaveChangesAsync();
return CreatedAtAction("GetChartsCategory", new { id = category.Id }, _mapper.Map<ChartsCategory, ChartsCategoryDto>(category));
}
else
{
_context.ChartsCategories.Update(category);
await _context.SaveChangesAsync();
return NoContent();
}
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Error updating data");
}
}
Which is supposed to handle both create and update of the entity named ChartsCategory
, which has a collection of Chart
s:
public partial class ChartsCategory
{
public ChartsCategory()
{
Charts = new HashSet<Chart>();
InverseParent = new HashSet<ChartsCategory>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Label { get; set; }
public string Icon { get; set; }
public int? ParentId { get; set; }
public virtual ChartsCategory Parent { get; set; }
public virtual ICollection<Chart> Charts { get; set; }
public virtual ICollection<ChartsCategory> InverseParent { get; set; }
}
- When I create a new entity - everything works as expected.
- When I update an existing entity - everything works as expected.
- Updating or creating a singular
Chart
within aChartCategory
- as expected. - Deleting a
Chart
within theChartsCategory
doesn't work.
I would expect the Update()
functionality to remove missing items.
I found this answer however it seems a little too explicit. I want to globally state that a descendant item must be sent, otherwise it should be removed.
Thanks