我有个模特:
public class VR
{
[Key]
public int ID { get; set; }
public string FullName { get; set; }
public string CreatedBy { get; set; }
public DateTime? Created { get; set; }
public string ModifiedBy { get; set; }
public DateTime? Modified { get; set; }
}我的控制器的Edit函数:
// POST: VRs/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(VR vR)
{
if (ModelState.IsValid)
{
var Result = (from c in _context.MyVR.Where(c => c.ID == vR.ID) select c).Single();
vR.Created = Result.Created;
vR.CreatedBy = Result.CreatedBy;
vR.ModifiedBy = User.Identity.Name;
vR.Modified = DateTime.Now;
_context.Update(vR);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(vR);
}我得到的错误如下:
无法跟踪实体类型'
UNTest.ViewModels.VR‘的实例,因为该类型的另一个具有相同键的实例已经被跟踪。对于新的实体,请考虑使用IIdentityGenerator生成唯一的键值。
发布于 2016-03-13 05:02:29
您可以附加该实体以避免从DB (保存性能)加载它,并且只更新您想要的字段。
这也避免了当您从DB (Result)加载实例并使用相同Id (vR)跟踪另一个实例导致异常时的代码问题。
// POST: VRs/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(VR vR)
{
if (ModelState.IsValid)
{
//Attach the instance so that we don't need to load it from the DB
_context.MyVR.Attach(vR);
vR.ModifiedBy = User.Identity.Name;
vR.Modified = DateTime.Now;
//Specify the fields that should be updated.
_context.Entry(vR).Property(x => x.ModifiedBy).IsModified = true;
_context.Entry(vR).Property(x => x.Modified).IsModified = true;
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(vR);
}另一种指定不应更新的字段的方法。
// POST: VRs/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(VR vR)
{
if (ModelState.IsValid)
{
//Attach the instance so that we don't need to load it from the DB
_context.Entry(vR).State = EntityState.Modified;
vR.ModifiedBy = User.Identity.Name;
vR.Modified = DateTime.Now;
//Specify the fields that should not be updated.
_context.Entry(vR).Property(x => x.Created).IsModified = false;
_context.Entry(vR).Property(x => x.CreatedBy).IsModified = false;
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(vR);
}如果使用视图模型,可以使用new操作符创建数据模型并复制要更新的字段:
// POST: VRs/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(VRViewModel vRVM)
{
if (ModelState.IsValid)
{
VR vR = new VR();
//Attach the instance so that we don't need to load it from the DB
_context.MyVR.Attach(vR);
//Set the Id for your model.
vR.Id = vRVM.Id;
//Let's say you also want to update this field from the VM
vR.FullName = vRVM.FullName;
vR.ModifiedBy = User.Identity.Name;
vR.Modified = DateTime.Now;
//Specify the fields that should be updated.
_context.Entry(vR).Property(x => x.ModifiedBy).IsModified = true;
_context.Entry(vR).Property(x => x.Modified).IsModified = true;
_context.Entry(vR).Property(x => x.FullName).IsModified = true;
_context.SaveChanges();
return RedirectToAction("Index");
}
//create your new view model and return it. For demonstration purpose, I return the same view model, in your real code, you can adjust it.
return View(vRVM);
}发布于 2016-03-13 04:57:35
您已经从上下文中检索了数据实体,因此将对其进行跟踪。然后,您就无法在相同的上下文中跟踪具有相同ID的其他实体。
更改POST方法以从视图模型更新数据模型,并保存数据模型
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(VR vR)
{
if (ModelState.IsValid)
{
var result = (from c in _context.MyVR.Where(c => c.ID == vR.ID) select c)
.FirstOrDefault(); // use FirstOrDefault() to prevent an exception if the user changes you input for the ID.
if (result != null)
{
result.FullName = vR.FullName;
result.ModifiedBy = User.Identity.Name;
result.Modified = DateTime.Now;
_context.Update(result);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(vR);
}https://stackoverflow.com/questions/35966561
复制相似问题