public bool DeleteCategoryItem(string name) { bool flag = false; using (var context = new ItemAPIContext()) { Item.Domain.Models.Category category = context.Category.Where(x => x.Name.Contains(name)).Include(x => x.SubCategory).FirstOrDefault(); if (category != null) { //We can use on delete cascade also. context.Category.Remove(category); context.SubCategory.RemoveRange(category.SubCategory); foreach (var item in category.SubCategory) { List <Item.Domain.Models.Item> Items = context.Item.Where(x => x.SubCategoryId == item.Id).ToList(); context.Item.RemoveRange(Items); } if (context.SaveChanges() > 0) { flag = true; } } } return(flag); }
public List <Item.Domain.Models.Item> GetItemsByname(string name) { using (var context = new ItemAPIContext()) { if (!string.IsNullOrEmpty(name)) { return(context.Item.Where(x => x.Name.Contains(name)).Include(x => x.SubCategory).ThenInclude(x => x.Category).ToList()); } return(context.Item.Include(x => x.SubCategory).ThenInclude(x => x.Category).ToList()); } }