尝试通过存储库编辑数据库中的数据时出现异常
Getting exeption when trying to edit data in my database through repository
所以我得到了这个例外
"Attaching an entity of type 'TimeTrackerProjectV2.Models.Project' failed because another entity of the same type already has the same primary key value"
当我尝试通过我的网络应用程序编辑我的数据库中的数据时。
控制器代码:
/ 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();
}
我已经在互联网上搜索过了,但我发现有相同例外的每个人都有它,因为他们使用的是 attache,所以它并不真正适用于我的情况。
发生这种情况是因为 ef 没有跟踪您的实体项目,所以它假设这是一个新实体,并且它附加它而不是更新它
你应该做的是,获取实体,更新值,然后使用 entry<>.state
像这样
// Get By ID
var TheEntity = db.Projects.FirstOrDefault(x => x.ID = project.ID);
//Update Values like
TheEntity.someProperty = project.someProperty ;
//Do it for all and then update the state of TheEntity
db.Entry(TheEntity).State = EntityState.Modified;
所以我得到了这个例外 "Attaching an entity of type 'TimeTrackerProjectV2.Models.Project' failed because another entity of the same type already has the same primary key value" 当我尝试通过我的网络应用程序编辑我的数据库中的数据时。
控制器代码:
/ 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();
}
我已经在互联网上搜索过了,但我发现有相同例外的每个人都有它,因为他们使用的是 attache,所以它并不真正适用于我的情况。
发生这种情况是因为 ef 没有跟踪您的实体项目,所以它假设这是一个新实体,并且它附加它而不是更新它 你应该做的是,获取实体,更新值,然后使用 entry<>.state
像这样
// Get By ID
var TheEntity = db.Projects.FirstOrDefault(x => x.ID = project.ID);
//Update Values like
TheEntity.someProperty = project.someProperty ;
//Do it for all and then update the state of TheEntity
db.Entry(TheEntity).State = EntityState.Modified;