So im getting this exeption "Attaching an entity of type 'TimeTrackerProjectV2.Models.Project' failed because another entity of the same type already has the same primary key value" When i try to edit data in my database through my webapplication.
Controller code:
/ GET: Projects/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
return View(Repositories.ProjectsRepository.GetProject(id));
}
// POST: Projects/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "IdProject,Name,Number,Active")] Project project)
{
if (ModelState.IsValid)
{
Repositories.ProjectsRepository.EditProjects(project);
Repositories.ProjectsRepository.SaveProject();
return RedirectToAction("Index");
}
return View(project);
}
Repository code;
public static void EditProjects(Project project)
{
db.Entry(project).State = EntityState.Modified;
}
public static void SaveProject()
{
db.SaveChanges();
}
I already searched it on the internet but everyone that i found with the same exeption had it because they were using attache so it didnt really apply to my case.