简体   繁体   中英

How do I get the ID from the first table to record the second?

How do I get the ID from the first table to record the second? Recalling that the id and generated automatic.

 [HttpPost]
    public ActionResult Create(Chamados chamados, InteracoesChamados interacoesChamados)
    {
        if (ModelState.IsValid)
        {
            db.Chamados.Add(chamados);
            //interacoesChamados.ChamadoId = chamados.ChamadoId;  
            db.InteracoesChamados.Add(interacoesChamados);

            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(chamados);
    }

The comment and exactly what I want to do. I need get the code from the first table and save the second record. What is the best way?

From looking at the code I assume that IteracoesChamados is a child of Chamados. If so, then set

iteracoesChamados.Chamados = chamados

The ID will be assigned automatically. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM