"同じタイプの別のエンティティが既に同じ主キー値を持っているため、タイプ 'TimeTrackerProjectV2.Models.Project' 私のウェブアプリケーションを通じてデータベースのデータを編集しようとしています。リポジトリを介してデータベースのデータを編集しようとすると例外が発生する
コントローラコード:
/ 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);
}
リポジトリコードと
public static void EditProjects(Project project)
{
db.Entry(project).State = EntityState.Modified;
}
public static void SaveProject()
{
db.SaveChanges();
}
私はすでに、インターネット上でそれを検索するが、それは本当に私の場合には適用されdidntのように、彼らはアタッシュケースを使用していたので、私は同じexeptionで見つかった誰もがそれを持っていました。
のような使用エントリは、私は、これはあなたの状況は、各リクエストの後に配置されたばかりではないとしなければならない賭けています。それぞれを 'using'ステートメントでインスタンス化することでこれをテストできます。 [this](https://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-anentity-of-type-modelname-failed-because-anotherentent) –