我现在一直在四处寻找。我尝试了几种解决方案,包括捕获 OptimisticConcurrency 并添加:
“@Html.HiddenFor(model => model.OrganizationID)”
到我的删除页面。我的索引(列表)、创建和编辑页面就像一个魅力。但是,当我去删除一行时,它给了我以下错误:
DbUpdateConcurrencyException
存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体后,实体可能已被修改或删除。刷新 ObjectStateManager 条目。
我按照教程构建了 Database First 应用程序。目前,我只是从我的组织表中引入数据,直到我可以让它顺利运行。我的组织模型如下所示(从“添加代码生成项”自动生成):
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace VAGTC.Models
{
using System;
using System.Collections.Generic;
public partial class Organization
{
public Organization()
{
this.Contact_Title = new HashSet<Contact_Title>();
this.Organization_Address = new HashSet<Organization_Address>();
this.Organization_Business_Type = new HashSet<Organization_Business_Type>();
this.Organization_Country = new HashSet<Organization_Country>();
this.Organization_Email = new HashSet<Organization_Email>();
this.Organization_Membership = new HashSet<Organization_Membership>();
this.Organization_Notes = new HashSet<Organization_Notes>();
this.Organization_Phone = new HashSet<Organization_Phone>();
this.Organization_Website = new HashSet<Organization_Website>();
this.Contacts = new HashSet<Contact>();
this.Organization_Industry_Code = new HashSet<Organization_Industry_Code>();
}
public int OrganizationID { get; set; }
public string Name { get; set; }
public virtual ICollection<Contact_Title> Contact_Title { get; set; }
public virtual ICollection<Organization_Address> Organization_Address { get; set; }
public virtual ICollection<Organization_Business_Type> Organization_Business_Type { get; set; }
public virtual ICollection<Organization_Country> Organization_Country { get; set; }
public virtual ICollection<Organization_Email> Organization_Email { get; set; }
public virtual ICollection<Organization_Membership> Organization_Membership { get; set; }
public virtual ICollection<Organization_Notes> Organization_Notes { get; set; }
public virtual ICollection<Organization_Phone> Organization_Phone { get; set; }
public virtual ICollection<Organization_Website> Organization_Website { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
public virtual ICollection<Organization_Industry_Code> Organization_Industry_Code { get; set; }
}
}
这是我的组织控制器中的 Delete ActionResult:
//
// GET: /Organization/Delete/5
public ActionResult Delete(int id)
{
using (var db = new VAGTCEntities())
{
return View(db.Organizations.Find(id));
}
}
//
// POST: /Organization/Delete/5
[HttpPost]
public ActionResult Delete(int id, Organization organization)
{
try
{
// TODO: Add delete logic here
using (var db = new VAGTCEntities())
{
db.Entry(organization).State = System.Data.EntityState.Deleted;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
在我的索引页面上,它声明了主键:
@Html.ActionLink("Delete", "Delete", new { id=item.OrganizationID })
出于好奇,我决定尝试添加
“@Html.HiddenFor(model => model.OrganizationID)”
到底部,在 BeginForm 下,而不是在字段集和图例标签下的页面顶部:
@using (Html.BeginForm()) {
<p>
@Html.HiddenFor(model => model.OrganizationID)
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
低头看——它奏效了。我还想发这个!也许其他人会找到它,它会帮助他们。虽然我不知道 100% 为什么会这样——有人可以解释一下吗?