public ActionResult Event_History(int id = 0)
{
//set into false the active flag of the event
Events_Info_tbl events = db.Events_Info_tbl.Find(id);
events.is_active = false;
db.Entry(events).State = EntityState.Modified;
//set the category under this event into inactive
List<Events_Category_tbl> category = new List<Events_Category_tbl>();
category = db.Events_Category_tbl.Where(x=>x.events_info_id==id).ToList();
foreach(var i in category){
Events_Category_tbl cat = new Events_Category_tbl();
cat.is_active = false;
db.Entry(cat).State = EntityState.Modified;
}
db.SaveChanges();
TempData["MessageAlert"] = "Event is save in history!";
return RedirectToAction("Index");
}
Iam計劃設置爲非活動狀態,所有類別都屬於該特定事件,但是當我嘗試運行此代碼時顯示錯誤「具有相同鍵的對象已存在於ObjectStateManager。ObjectStateManager不能使用相同的密鑰跟蹤多個對象。「指向我的這部分代碼db.Entry(cat).State = EntityState.Modified;
更新數據庫中的數據列表
您不需要手動更改狀態,它由EF處理 – Peyman 2015-03-25 00:18:56