Error trying to update data in Asp.Net MVC - db. Entry(Lada) application. State = Entitystate.Modified

Asked

Viewed 29 times

1

I am having problems at the time of editing, more specifically in the lines below,

db. Entry(Lada). State = Entitystate.Modified;

db Savechanges.();

the update that precedes the lines above, occurs normally, where below indicate.

Data update occurs normally

db. Entry(seq). State = Entitystate.Modified;

db Savechanges.();

Error that occurs in the image attached to this post. Erro que ocorre esta na imagem anexa a este post.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(FormCollection form)
    {
        var sequenciaViewModel = new SequenciaViewModel();

        if (ModelState.IsValid)
        {
            var seq = new Sequencia
            {
                SequenciaId = Convert.ToInt16(form["Sequencia.SequenciaId"].ToString()),
                DataAbate = Convert.ToDateTime(form["Sequencia.DataAbate"].ToString()),
                NumeroLote = Convert.ToInt16(form["Sequencia.NumeroLote"].ToString()),
                NumeroSequencia = Convert.ToInt16(form["Sequencia.NumeroSequencia"].ToString())
            };
            db.Entry(seq).State = EntityState.Modified;
            db.SaveChanges();

            var aff = db.SequenciaLados.Where(s => s.SequenciaId == seq.SequenciaId).ToList();

            foreach (var item in aff)
            {
                if (item.Lado.Nome.Equals("A"))
                {
                    var ladA = new Lado
                    {
                        LadoId = item.LadoId,
                        Nome = form["Lado_A.Nome"].ToString(),
                        CamaraId = Convert.ToInt16(form["Camara_Lado_A"].ToString()),
                        TipoLadoId = Convert.ToInt16(form["Tipo_Lado_A"].ToString())
                    };
                    db.Entry(ladA).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else if (item.Lado.Nome.Equals("B"))
                {
                    var ladB = new Lado
                    {
                        LadoId = item.LadoId,
                        Nome = form["Lado_B.Nome"].ToString(),
                        CamaraId = Convert.ToInt16(form["Camara_Lado_B"].ToString()),
                        TipoLadoId = Convert.ToInt16(form["Tipo_Lado_B"].ToString())
                    };
                    db.Entry(ladB).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }

            return RedirectToAction("Index");
        }
        return View();
    }

1 answer

-1


I managed to solve, and so I share with the community

sequenciaViewModel.Lado_A = new Lado
{
     LadoId = item.LadoId,
     Nome = form["Lado_A.Nome"].ToString(),
     CamaraId = Convert.ToInt16(form["Camara_Lado_A"].ToString()),
     TipoLadoId = Convert.ToInt16(form["Tipo_Lado_A"].ToString())
};
var local = db.Set<Lado>().Local.FirstOrDefault(f => f.LadoId == item.LadoId);
db.Entry(local).State = EntityState.Detached;
db.Entry(sequenciaViewModel.Lado_A).State = EntityState.Modified;
db.SaveChanges();

The magic happens with the lines below:

var local = db. Set().local.Firstordefault(f => f.Ladoid == item.Ladoid);

db. Entry(local). State = Entitystate.Detached;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.