2012-08-28 67 views
-1

我有两个表,我有一个按钮控件,当我点击按钮时,table2中的记录添加到table1,并且该记录必须在table2中删除。 我写了像如何在一次添加查询添加和删除操作

public ActionResult Accept(int id, string selectedVal)       //To update Accept status 
     { 
      var retrieveID = (from i in dbContext.groups 
           where i.groups_name == selectedVal 
           select new { groups_id = i.Usr_contacts_groups_id }).FirstOrDefault(); 

      var currentId = 1; 
      var deleteQuery = dbContext.requests.Where(i => i.from_usr_id == id && i.to_usr_id == id).FirstOrDefault(); 

      contact contactGroup = new contact(); 

      contactGroup.groups_id = retrieveID.groups_id; 
      contactGroup.groups_usr_id = currentId; 



      this.dbContext.Add(contactGroup); 
      this.dbContext.Delete(deleteQuery); 
      try 
      { 
       this.dbContext.SaveChanges(); 

      } 
      catch 
      { 
       return View(); 
      } 
      return Json(null); 
     } 

我可以添加在表1的记录,并在删除查询“ID”值表2删除多条记录。

+0

,问题是......? –

+0

只删除那个特定记录不是多个记录 – steve

+0

'table2'的主键是什么? –

回答

0

,在表2的记录添加到表1和记录在表2中删除

我会假设,用简单的英语:

,在table2记录应该添加到table1然后从table2中删除

换句话说,将记录table1转换为table2,对吧? (假设它是)

public ActionResult MoveRecord(int id) { 

    // get Table 2 record 
    var recordA = dbContext.table2.FirstOrDefault(x => x.bioId == id); 

    // if we did find a record 
    if (recordA != null) { 

     // let's add it into Table 1 
     var recordB = new Table2(); 
     recordB.id = recordA.id; 
     recordB.value = recordA.value; 

     // add to database 
     dbContext.table1.AddObject(recordB); 

     // now that we have it in the database, let's delete the original on 
     dbContext.table2.DeleteObject(recordA); 

     // save all changes 
     dbContext.SaveChanges(); 
    } 


    return RedirectToAction("Index"); 
} 
+0

没关系balexandre。 OP每10分钟改变一次他的想法。我认为这个问题与PK不匹配有关。 –