使用 ObjectContext 。 我想通过执行系统传递 SQL 查询, 因为我不想只为了在事后删除而检索所有相关实体 。
类别表如下:
CatID | CatName | ParentID
CatID 是父式ID FK 的主密钥
I am wishing to delete a category and also all those that are under it. Can be 2+ levels deep of sub cats, so different ParentID s
Thought I could do it as below and just set "cascade" on delete option for the foreign key in the database, but it won t let me and it does not appear to want to cascade delete down by using the CatID - ParentID relationship and the query gets stopped by this very FK constraint.
public RedirectToRouteResult DelCat(int CatID)
{
if (CatID != 0)
{
_db.ExecuteStoreCommand("DELETE FROM Categories WHERE CatID={0}", CatID);
_db.SaveChanges();
}
return RedirectToAction("CatManage");
}