刪除表實體問題

錯誤提示:

The object cannot be deleted because it was not found in the ObjectStateManager.

 

原来是因为DeleteObject方法必须是先关联实体,或者实体来自查询。

 

刪除集合:

List<UPLOAD_FILES> lstFile = comm.GetFiles(model.HRTrainings.FormNo);

 foreach (var item in lstFile)

 {

     model.file = item;

     db.UploadFiles.Attach(model.file);//此句為重點

     db.UploadFiles.Remove(model.file);

 }

 

 參照:http://www.cnblogs.com/Benjamin/archive/2012/10/24/2736739.html

你可能感兴趣的:(刪除表實體問題)