วันพุธที่ 8 กุมภาพันธ์ พ.ศ. 2560
Asp.net MVC - การลบ Catalog
เราต้องเข้าใจว่า ใน Table Product มี Catelog เป็น foreign key อยู่
ดังนั้น เมื่อ เราลบ catelog ไป มันจะเกิด error ขึ้น เพราะว่า Product ที่เป็น
catelog ชนิดนี้อยู่ หายไป
ดังนั้น ที่ function delete ใน CategoriesController
เราต้องเปลี่ยน product ที่ชนิด category นี้ ให้เป็น null
// POST: Categories/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Category category = db.Categories.Find(id);
foreach (var p in category.Products)
{
p.CategoryID = null;
}
db.Categories.Remove(category);
db.SaveChanges();
return RedirectToAction("Index");
}
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น