我已将对象附加到上下文,尽管我收到了错误Cannot remove an entity that has not been attached.
if (itemRemove != -1)
{
//var deleteDetails = DBContext.ProductCustomizationMasters.Where(p => p.ProductID == this.ProductID && p.CustomCategoryID == catId && p.CustomType == (short)catTypeId).Single();
var deleteDetails = DBContext.ProductCustomizationMasters.Single(p => p.ProductID == this.ProductID && p.CustomCategoryID == catId && p.CustomType == (short)catTypeId);
DBContext.ProductCustomizationMasters.Attach(deleteDetails);
DBContext.ProductCustomizationMasters.DeleteOnSubmit(deleteDetails);
RemoveCategoryItems(catId, catTypeId);
}
private void RemoveCategoryItems(int catId, CategoryType catTypeId)
{
switch (catTypeId)
{
case CategoryType.Topping:
(this.ToppingItems.Where(xx => xx.ToppingInfo.CatID == catId && xx.ProductID == this.ProductID).Single()).IsDefault = false;
FreeToppingItems.RemoveAll(x => x.ProductID == this.ProductID && x.ToppingInfo.CatID == catId);
break;
case CategoryType.Dressing:
(this.DressingItems.Where(xx => xx.DressingInfo.CatID == catId && xx.ProductID == this.ProductID).Single()).IsDefault = false;
FreeDressingItems.RemoveAll(x => x.ProductID == this.ProductID && x.DressingInfo.CatID == catId);
break;
case CategoryType.SpecialInstruction:
(this.InstructionItems.Where(xx => xx.InstructionInfo.CatID == catId && xx.ProductID == this.ProductID).Single()).IsDefault = false;
FreeInstructionItems.RemoveAll(x => x.ProductID == this.ProductID && x.InstructionInfo.CatID == catId);
break;
}
}