-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);
        }

我可以在 table1 中添加记录,并在删除查询中使用“id”值删除 table2 中的多个记录。

4

1 回答 1

0

table2 中的记录添加到 table1 并且该记录必须在 table2 中删除

我会用简单的英语假设:

该记录table2应添加到table1然后删除table2

换句话说,将记录table1into 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");
}
于 2012-08-28T12:12:40.307 回答