加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 实体框架删除子对象

发布时间:2020-12-15 22:26:59 所属栏目:asp.Net 来源:网络整理
导读:我有两个没有任何级联删除的表.我想删除所有子对象的父对象.我是这样做的 //get parent objectreturn _dataContext.Menu.Include("ChildMenu").Include("ParentMenu").Include("Pictures").FirstOrDefault(m = m.MenuId == id);//then i loop all child obje
我有两个没有任何级联删除的表.我想删除所有子对象的父对象.我是这样做的
//get parent object
return _dataContext.Menu.Include("ChildMenu").Include("ParentMenu").Include("Pictures").FirstOrDefault(m => m.MenuId == id);
//then i loop all child objects
var picList = (List<Picture>)menu.Pictures.ToList();

for (int i = 0; i < picList.Count; i++)
 {
  if (File.Exists(HttpContext.Current.Server.MapPath(picList[i].ImgPath)))
  {
     File.Delete(HttpContext.Current.Server.MapPath(picList[i].ImgPath));
  }
  if (File.Exists(HttpContext.Current.Server.MapPath(picList[i].ThumbPath)))
  {
     File.Delete(HttpContext.Current.Server.MapPath(picList[i].ThumbPath));
  }

  //**what must i do here?**
  //menu.Pictures.Remove(picList[i]);
  //                DataManager dm = new DataManager();
  //                dm.Picture.Delete(picList[i].Id);

  //menu.Pictures.de
  //_dataContext.SaveChanges();
  //picList[i] = null;

}

//delete parent object
_dataContext.DeleteObject(_dataContext.Menu.Include("ChildMenu").Include("ParentMenu")
    .Include("Pictures").FirstOrDefault(m => m.MenuId == id););
_dataContext.SaveChanges();

解决方法

这足以设置
<OnDelete Action="Cascade" />

对于主协会在模型的CSDL part中结束.在这种情况下,您的代码将起作用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读