7
I have a web application on . net 4.5.1 with MVC and Entity framework.
Error occurs in a basic CRUD.
I recover the object that will be the model sent to the view with the following code:
Professor professor = db.Pessoa
.Include(p => p.Agenda)
.Include(p => p.Contato)
.Include(p => p.Endereco)
.Include(p => p.SocioEconomico)
.Include(p => p.SocioEconomico.TelefoneMae)
.Include(p => p.SocioEconomico.TelefonePai)
.Include(p => p.Contato.Telefones)
.Where(p => p is Professor)
.Single(p => p.Id == id.Value) as Professor;
This object is sent to the view and then I receive the POST in order to update one or more fields.
In the POST you have the following code:
db.Entry(professor).State = EntityState.Modified;
db.SaveChanges();
But when going through the had that changes the entity’s status to "Modified" I get the following exception:
An Object with the same key already exists in the Objectstatemanager. The Objectstatemanager cannot track Multiple Objects with the same key.
If I retrieve the "Teacher" using the Find method professor = db.Find(id)
, update works properly, but I don’t have the "kids" objects from it.
I’ve seen several possible solutions, but none worked. Someone has had the same problem and can tell me which error?
Adding all controller method definitions as requested
GET
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Professor professor = db.Pessoa
.Include(p => p.Agenda)
.Include(p => p.Contato)
.Include(p => p.Endereco)
.Include(p => p.SocioEconomico)
.Include(p => p.SocioEconomico.TelefoneMae)
.Include(p => p.SocioEconomico.TelefonePai)
.Include(p => p.Contato.Telefones)
.Where(p => p is Professor)
.Single(p => p.Id == id.Value) as Professor;
if (professor == null)
{
return HttpNotFound();
}
return View(professor);
}
POST
public ActionResult Edit(Professor professor)
{
if (ModelState.IsValid)
{
db.Entry(professor).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(professor);
}
Try this: db.Professores.Attach(Entity); db. Entry(Entity). State = Entitystate.Modified; //where we assume q Teachers contain your db.Set<Teacher>
– iuristona
iuristona, had already seen this possible solution. I even tried again here, but the error that occurs is the same. Now on the line where I do "Attach".
– Carlos8k
This is very suspicious:
.Where(p => p is Professor).Single(p => p.Id == id.Value) as Professor
. Try trading for.SingleOrDefault(p => p is Professor && p.Id == id.Value)
.– Leonel Sanches da Silva
It is strange because the error occurs basically if you already have a Teacher object with the same key, in Objectstatemanager. Apparently you don’t, an alternative to trying to debug would be to inspect if you find a Teacher in db.Professores.Local.Firstordefault(m => m.Id == teacher.Id); The Local object contains the entities found in the Statemanager
– iuristona
Gypsy, what’s the difference? EF will interpret the two codes in the same way. It will only save one line. iuristona, really, in theory should only have one Teacher object in the Objectstatemanager. I will try this code and already put the result.
– Carlos8k
iuristona, the code returned null as expected. =/
– Carlos8k
I was very intrigued about what is causing the problem. Another attempt I would make, to detect if the problem is only with Teacher or with the children objects, would be to force a Clear in Objectstatemanager Professor: db.Professores.Local.Clear(); if the problem continues there is probably a duplicity in the Teacher’s children objects and this error is triggered when you attack Teacher and his children.
– iuristona
Another tip, q has nothing to do with the problem, is in the query. It would be more semantic you change to:
db.Pessoa
.OfType<Professor>()
 .Include(p => p.Agenda)
 .Include(p => p.Contato)
 .Include(p => p.Endereco)
 .Include(p => p.SocioEconomico)
 .Include(p => p.SocioEconomico.TelefoneMae)
 .Include(p => p.SocioEconomico.TelefonePai)
 .Include(p => p.Contato.Telefones)
 .Where(p => p.Id == id.Value)
 .FirstOrDefault()
– iuristona
It really gets more elegant and I avoid the "The Teacher". But back to the error... the ids of the children objects are zeroed. Could this be the mistake? If I’m changing, they should be with their respective ids, correct?
– Carlos8k
I’ll promote my comment as a response suggestion, okay?
– iuristona
Maybe you created partial class with the same namespace. .
– user26380