Error while editing Viewmodel data

Asked

Viewed 344 times

0

In project I have a Viewmodel and inside it my models and everything. What happens is that I am trying to edit the data that is already saved in the database.

I can bring the edited data from the view, but when actually saving, this error is generated:

An Exception of type 'System.Data.Entity.Infrastructure.Dbupdateconcurrencyexception' occurred in Entityframework.dll but was not handled in user code

Additional information: Store update, Insert, or delete statement affected an Unexpected number of Rows (0). Entities may have been modified or Deleted Since entities Were Loaded. See http://go.microsoft.com/fwlink/? Linkid=472540 for information on understanding and Handling optimistic concurrency exceptions.

I don’t know what I might be doing wrong when editing and saving this data...

Well, the POST action that edits this data, has the following code:

         [HttpPost]
    public ActionResult Edit(AnamineseViewModel anamneseViewModel)
    {
        if (ModelState.IsValid)
        {
            AnaAnamineseAlimentar anamnese = db.AnaAnamineseAlimentar.Find(anamneseViewModel.AnaAnamineseAlimentar.AnaId);
            CliCliente cliente = db.CliCliente.Find(anamneseViewModel.CliCliente.CliId);
            RecRecordatorio recordatorio = db.RecRecordatorio.Find(anamneseViewModel.RecRecordatorio.AnaId);
            List<RefRefeicao> refeicao = anamneseViewModel.RefRefeicao;
            List<QfaQuestionarioFrequenciaAlimentar> qfa = anamneseViewModel.QfaQuestionarioFrequenciaAlimentar;

            cliente = anamneseViewModel.CliCliente;
            anamnese = anamneseViewModel.AnaAnamineseAlimentar;
            recordatorio = anamneseViewModel.RecRecordatorio;
            refeicao = anamneseViewModel.RefRefeicao;
            qfa = anamneseViewModel.QfaQuestionarioFrequenciaAlimentar;

            var cliCliente = db.Set<CliCliente>().Local.FirstOrDefault(f => f.CliId == anamneseViewModel.CliCliente.CliId);

            if (cliCliente != null)
            {
                db.Entry(cliCliente).State = EntityState.Detached;
            }

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

            var anaAnamnese = db.Set<AnaAnamineseAlimentar>().Local.FirstOrDefault(f => f.AnaId == anamneseViewModel.AnaAnamineseAlimentar.AnaId);

            if (anaAnamnese != null)
            {
                db.Entry(anaAnamnese).State = EntityState.Detached;
            }

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

            var recrecordatorio = db.Set<RecRecordatorio>().Local.FirstOrDefault(f => f.AnaId == anamneseViewModel.RecRecordatorio.AnaId);

            if (recrecordatorio != null)
            {
                db.Entry(recrecordatorio).State = EntityState.Detached;
            }

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

            for (int i = 0; i < refeicao.Count; i++)
            {
                var refRefeicao = db.Set<RefRefeicao>().Local.FirstOrDefault(f => f.RefId == anamneseViewModel.RefRefeicao[i].RefId);

                if (refRefeicao != null)
                {
                    db.Entry(refRefeicao).State = EntityState.Detached;
                }

                db.Entry(refeicao[i]).State = EntityState.Modified;
            }

            for (int i = 0; i < qfa.Count; i++)
            {
                var qfaQfa = db.Set<QfaQuestionarioFrequenciaAlimentar>().Local.FirstOrDefault(f => f.QfaId == anamneseViewModel.QfaQuestionarioFrequenciaAlimentar[i].QfaId);

                if (qfaQfa != null)
                {
                    db.Entry(qfaQfa).State = EntityState.Detached;
                }

                db.Entry(qfa[i]).State = EntityState.Modified;
            }

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

        return View(anamneseViewModel);
    }

I can return the data, but on db.SaveChanges() the error is generated. How could I solve?

  • Have you checked whether anamneseViewModel is not void?

  • Already... It is not null... It is completely filled...

  • It seems that you are editing several entities that are correlated, and the update of one reflects cascading in the others, already tried to give an individual savechange in each to see if it runs?

1 answer

0


I solved the mistake.

It was generated by the fact that at the time of saving, I was not with the Ids, both of the table, as well as the relationships between the entities.

As there was no reference, the error was generated.

To solve, I adjusted the view and in it, I pass the corresponding Ids.

Browser other questions tagged

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