public void RemoveEntry(int ImageIndex) { PicTable EntryToRemove = dbcontext.PicTableSet.Where(obj => obj.ID_IMG == ImageIndex).Include(p => p.Categories).Include(p => p.Person).FirstOrDefault(); //Categories CatRemove = dbcontext.CategoriesSet.Where(obj => obj.PicTable == EntryToRemove).FirstOrDefault(); dbcontext.PicTableSet.Remove(EntryToRemove); dbcontext.SaveChanges(); }
public void AddEntry(string name, string path, string description, string location, DateTime dt, List <string> categories, List <string> peoplys) { try { PicTable newPic = new PicTable { Name_img = name, Path = path, Location = location, Description = description, Data_create = dt }; foreach (var index in categories) { if (!isInCategories(index)) { Categories cat = new Categories { CategoryName = index }; newPic.Categories.Add(cat); cat.PicTable.Add(newPic); } else { Categories cat = dbcontext.CategoriesSet.FirstOrDefault(obj => obj.CategoryName == index); newPic.Categories.Add(cat); cat.PicTable.Add(newPic); } } foreach (var nameP in peoplys) { if (!existPerson(nameP)) { Person pers = new Person { FirstName = nameP, }; newPic.Person.Add(pers); pers.PicTable.Add(newPic); } else { Person pers = dbcontext.PersonSet.FirstOrDefault(obj => obj.FirstName == nameP); newPic.Person.Add(pers); pers.PicTable.Add(newPic); } } dbcontext.PicTableSet.Add(newPic); dbcontext.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.Message); throw; } }