According to the scenario below, I would like to update child entities from parent entity.
I approached the problem as follows.
- Child entities of parent delete from database.
- New child entities of parent add from database.
Is that approach true way ?
public int SaveBasket(Addition addition)
{
var entity = ApplicationDbContext.Additions.Include(x => x.Basket).SingleOrDefault(x => x.AdditionId == addition.AdditionId);
//Remove Basket
if (entity.Basket.Count > 0)
{
foreach (var item in entity.Basket)
{
context.Entry(item).State = EntityState.Deleted;
}
ApplicationDbContext.SaveChanges();
}
//Add new basket entities from posting json data
entity.Basket = addition.Basket;
return ApplicationDbContext.SaveChanges();
}