Edit / Refresh - ASP.NET MVC

0

Good afternoon! I'm trying to edit from "João Torres" to "João Torres Moreira", however the error message below appears.

  

The repository update, insertion, or deletion statement affected   an unexpected number of rows (0). The entities may have been   modified or deleted after loading. Update the   ObjectStateManager.

Follow the image below.

MyCodeMedicosController.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;usingCadeMeuMedico.Models;usingSystem.Data.Entity;usingSystem.Data;namespaceCadeMeuMedico.Controllers{publicclassMedicosController:Controller{privateCadeMeuMedicoBDEntitiesdb=newCadeMeuMedicoBDEntities();////GET:/Medicos/publicActionResultIndex(){varmedicos=db.Medicos.Include(m=>m.Cidades).Include(m=>m.Especialidades).ToList();returnView(medicos);}publicActionResultAdicionar(){ViewBag.IDCidade=newSelectList(db.Cidades,"IDCidade", "Nome");
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome");
            return View();
        }

        [HttpPost]
        public ActionResult Adicionar(Medicos medicos)
        {
            if (ModelState.IsValid)
            {
                db.Medicos.Add(medicos);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome", medicos.IDCidade);
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome", medicos.IDEspecialidade);
            return View(medicos);

        }

        [HttpGet]
        public ActionResult Editar(long id)
        {
            Medicos medicos = db.Medicos.Find(id);
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome", medicos.IDCidade);
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome", medicos.IDEspecialidade);
            return View(medicos);
        }

        [HttpPost]
        public ActionResult Editar(Medicos medicos)
        {
            if (ModelState.IsValid)
            {
                db.Entry(medicos).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDCidade = new SelectList(db.Cidades, "IDCidade", "Nome", medicos.IDCidade);
            ViewBag.IDEspecialidade = new SelectList(db.Especialidades, "IDEspecialidade", "Nome", medicos.IDEspecialidade);
            return View(medicos);
        }

        [HttpPost]
        public string Excluir(long id)
        {
            try
            {
                Medicos medicos = db.Medicos.Find(id);
                db.Medicos.Remove(medicos);
                db.SaveChanges();
                return Boolean.TrueString;
            }
            catch
            {
                return Boolean.FalseString;
            }
        }
    }
}
    
asked by anonymous 09.04.2018 / 19:34

1 answer

0

There are some possibilities for this error to be happening ...

1st - E.F is not identifying that there is a change in your entity because it may not actually exist in the bank.

2nd - If the first possibility is discarded, you can first consult the entity, placing it in the context of E.F, change what comes from its view and then update the object.

By eliminating these two possibilities, I believe that solves the problem. One approach you can also use for testing is as follows:

if (ModelState.IsValid)
{
    Db.Entry(medico).State = medico.MedicoId == null ? 
    EntityState.Added : EntityState.Modified;

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

If there is no MedicoId the object receives the Status added which causes the EF context to interpret that it is adding this entity, otherwise, / em> Modified .

Through Debug you can also check if the entity is arriving for the completed actuation correctly. If that does not solve, post also, as your entity is coming, adding more details of the problem makes it easier to reach a resolution.

    
11.04.2018 / 01:09