【问题标题】:Unable to remove the row from table in which foreign key exists in MVC无法从 MVC 中存在外键的表中删除行
【发布时间】:2013-10-25 03:18:06
【问题描述】:

在我的 mvc 应用程序中,我有 2 个名为的表: 与外键关系相关的详细信息(srNo,ID,工作),主(ID,名称,计划)使用“ID”字段从详细信息到主信息。 “ID”字段是主表的主键。 “srNo”字段是明细表的主键。

从“ID”字段中,这两个表是通过外键关系连接的。

现在的问题是: 在向数据库添加任何行时,我们首先在主表中输入,然后在详细表中输入。 有时由于某些异常,在主表中成功添加行但无法在明细表中添加时。然后我想从主表执行回滚。

但是当我想删除添加到主表中的最近添加的 ID 值(grom linq to sql)的行时,它给了我一个例外,即外键 Realtion Ship 在那里.....

而当时明细表中没有那个id字段的行。

谢谢

【问题讨论】:

    标签: linq-to-sql


    【解决方案1】:

    这里是代码

    master objmaster=新的master(); objmaster.id =计数; objmaster.name=name1; .. objEntities.AddObject("master", objmaster); objEntities.SaveChanges(); 尝试 { 细节 objDetail = 新细节() master objMaster = (from r in objEntities.master where r.Id == count select r).First(); objDetail.master = objMaster; // 假设我得到任何(格式,这里的空指针异常) objDetail.folow = sfields[0]; //如果出现异常,下面的代码将不会被执行

    objEntities.AddObject("detail", objDetail); objEntities.SaveChanges(); } 捕捉(异常前) {

    //这里我想删除我在主表中添加的行: // 我为此编写代码 master objMasters= (from r in objEntities.master where r.Id == count select r).ToList(); objEntities.DeleteObject(objMasters); objEntities.SaveChanges(); }

    【讨论】:

      【解决方案2】:

      它也将数据插入到子表中。首先,您必须使用 deleteallonsubmit(detail) 从详细表中删除,然后从主表中删除。你能解释一下异常是什么意思,是错误还是你的验证

      public ActionResult CreateRec(Master _Master, IEnumerable<Detail> Detaildata,FormCollection collection)
      {
        MasterRepo _MasterRepo = new MasterRepo();
        if (ModelState.IsValid)
              {
                  if (_detail != null)
                  {
                      foreach (var objdetail in DetailData)
                      {
                          Detail _Detail = new Detail();
                          _Detail.FirstField = objdetail.FirstField;
                         _Detail.SecondField = objdetail.SecondtField;
                          _Master.Detail.Add(_Detail);
                      }
                  }
                _MasterRepo.Save();  
              }
      
      }
      

      【讨论】:

      • 我要从主表中删除时,子表中没有插入数据:
      • 它没有给出任何验证错误,它给出了无法删除存在外键关系的错误。
      • 你是在调用一个 submitchanges 的 child 和 parent 还是两者都分别保存到数据库
      • 我添加了我的新答案,因为评论字段容量较小。请看那个答案。和帮助
      • 请在发布代码时编辑您的答案,按代码按钮格式化,使其可读
      【解决方案3】:

      我认为您需要将它们一起提交,以便它们处于同一事务中。然后不需要编写回滚代码,因为如果细节失败,主节点将不会被插入。 试试这个:

      master objmaster= new master(); 
      objmaster.id =count; 
      objmaster.name= name1; 
      .. 
      objEntities.AddObject("master", objmaster); 
      // == removed save changes call here ==
      detail objDetail = new detail() 
      // == no need to reselect master from the database, you already have it
      // == so just assign it, and that will take care of the references/foreign-key
      objDetail.master = objMaster; // Suppose I get any (format, null pointer exception here) 
      objDetail.folow = sfields[0]; //In case of exception this below code will not be excecuted
      
      objEntities.AddObject("detail", objDetail); 
      // == save at the end ==
      objEntities.SaveChanges(); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多