我有两个表,我有一个按钮控件,当我点击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);
}
我能够在table1中添加记录并删除table2中的多个记录,并在删除查询中使用“id”值。
答案 0 :(得分:0)
table2中的记录添加到table1,该记录必须在table2中删除
我将用简单的英语假设:
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");
}