I am trying to remove category object from News but it doesn't work
My code looks like this:
var OriginalCategoriesIds = db.News.Where(w => w.NewsId == 1)
.SelectMany(v => v.Categories)
.ToList();
News NewsToUpdate = new News() { NewsId = 1 };
db.News.Attach(NewsToUpdate);
foreach (var category in OriginalCategoriesIds)
{
if (!model.SelectedCategoriesIds.Contains(category.CategoryId))
{
NewsToUpdate.Categories.Remove(category);
}
}
db.SaveChanges();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…