为什么有时候在EF 中删除不了表呢?
先上代码:
var deleteObj = adminDpProEntity.ToTAdminDpPro(); _db.t_sys_admin_dp_product.DeleteObject(deleteObj);
报错:The object cannot be deleted because it was not found in the ObjectStateManager。
一搜索:原来是因为DeleteObject方法必须是先关联实体,或者实体来自查询。
代码改为
_db.t_sys_admin_dp_product.Attach(deleteObj);
_db.ObjectStateManager.ChangeObjectState(deleteObj, EntityState.Deleted);
就OK了