In design I have a ViewModel and inside it my models and everything. What happens is that I'm trying to edit data that is already saved in the database.
I can bring the edited data of the view, but when I actually save it, 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 (0). Entities may have been modified or deleted since classes were loaded. See link for information on understanding and handling optimistic concurrency exceptions.
I do not know what I might be doing wrong when editing and saving these data ...
Well, the POST action that does the editing of 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 in db.SaveChanges()
the error is generated .. How could I solve it?