1
Guys I’m having a bit of a problem, I’m trying to do an update using the Entity Framework, but when it arrives in Savechanges(), the following error appears.
An Object with the same key already exists in the Objectstatemanager. The Objectstatemanager cannot track Multiple Objects with the same key.
From what I found on the internet has something to do with the instance of my context, I tried to make a new instance but it still didn’t work.
Follows the code:
[HttpPost]
public ActionResult EditPassword([Bind(Include = "intIDUsuario,strNome,strUsuario,strSenha,bitStatus,strTelefone,strEmail,intIDPerfil,NovaSenha,ConfirmaSenha")] Usuario usuario)
{
try
{
var query2 = from q in db.Usuario where q.intIDUsuario == usuario.intIDUsuario select q.intIDUsuario;
int id1 = query2.FirstOrDefault();
string pass = Cryptography.Encrypt(usuario.strSenha);
var user = db.Usuario.ToList().Where(x => x.strSenha == pass && usuario.NovaSenha == usuario.ConfirmaSenha).Count() > 0;
if (user)
{
usuario.strSenha = Cryptography.Encrypt(usuario.NovaSenha);
db.Entry(usuario).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index","Suporte");
}
return RedirectToAction("Index", "Suporte");
}
catch
{
ModelState.AddModelError("","Ocorreu um erro!!!");
}
return View(usuario);
}
On the line
var query2 = from q in db.Usuario where q.intIDUsuario == usuario.intIDUsuario select q.intIDUsuario;

 int id1 = query2.AsNoTracking().FirstOrDefault();
The following error
 appears;The type 'int' must be a Reference type in order to use it as Parameter’T' in the Generic type or method 'System.Data.Entity.QueryableExtensions.Asnotracking<T>(System.Linq.Iqueryable<T>)'– Diego Dias
@Diegodias Cara, I forgot to edit the answer for you on the day! Really bad. Now it’s OK.
– Leonel Sanches da Silva