cómo escribir una consulta para agregar y eliminar operaciones a la vez [cerrado]
Frecuentes
Visto 997 veces
-1
I have two tables,I have a button control,when i click on the button that record in table2 added to table1 and that record has to delete in table2. I wrote like
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);
}
I am able to add record in the table1 and deleting multiple records in table2 with "id" value in delete query.
1 Respuestas
0
that record in table2 added to table1 and that record has to delete in table2
I will assume, in plain English:
That record in
table2
should be added intotable1
and then deleted fromtable2
en otras palabras, Move record de table1
dentro table2
, ¿derecho? (let's assume it is)
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");
}
Respondido 28 ago 12, 13:08
Never mind balexandre. The OP changes his mind every 10 min. I think the issue's got to do with PK mismatches. - gert arnold
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net-mvc-3 visual-studio-2010 linq or haz tu propia pregunta.
delete only that particular record not multiple records - steve
What's the primary key of
table2
? - Gert ArnoldI have edited my question,My primary keys are userId ,bioId in table2 - steve
What is your question? And is this your actual code? It won't compile. - Dennis Traub
Please show your mapping code. - Gert Arnold