微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有两个没有任何级联删除的表.我想删除所有子对象的父对象.我是这样做的
//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中结束.在这种情况下,您的代码将起作用.

原文地址:https://www.jb51.cc/aspnet/249173.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐