I have an object MyProduct which has a property - a List of type List<Category>.
When I insert MyProduct with dbContext.SaveChanges() to the db the product gets inserted into the relevant table in the db and the list of properties get inserted into a linked table in the database which has double primary key eg. ProductId and CategoryId - for each category in the List<Category>.
When I'm trying to update the product and assign the same categories to MyProduct then product gets updated but the context is trying to insert again same category ids values into the linked table.
MyProduct product = _repo.GetProduct(productDto.ProductId);
product.Categories = categories;
_dbContext.SaveChanges();
What I need it to do is rather delete existing pairs ProductId + CategoryId and insert them again according to the values provided in List<Category> in MyProduct
or similar, but don't want it to attempt to insert same values again.
How to achieve it?