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

1

I'm having problems at the time of editing, more specifically in the line below,

db.Entry (ladA) .State = EntityState.Modified;

db.SaveChanges ();

The update that precedes the above lines usually occurs, where I indicate below.

Data update usually occurs

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

db.SaveChanges ();

Error that occurs in the image attached to this post.

[HttpPost][ValidateAntiForgeryToken]publicActionResultEdit(FormCollectionform){varsequenciaViewModel=newSequenciaViewModel();if(ModelState.IsValid){varseq=newSequencia{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();
    }
    
asked by anonymous 10.06.2018 / 01:10

1 answer

-1

I was able to resolve, and thus 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:

local var = db.Set (). Local.FirstOrDefault (f => f.LedId == item.LoadId);

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

    
10.06.2018 / 03:43