2012-11-08 31 views
1

我一直在尋找一些東西。我曾嘗試涉及捕捉OptimisticConcurrency並添加一對夫婦的解決方案:修改數據時出現DbUpdateConcurrencyException

「@ Html.HiddenFor(型號=> model.OrganizationID)」

我刪除頁面。我的索引(列表),創建和編輯頁面就像一個魅力。然而,當我去刪除一行它給了我下面的錯誤:

DbUpdateConcurrencyException
商店更新,插入或刪除語句影響了意外 的行數(0)。自從裝載了實體 以來,實體可能已被修改或刪除。刷新ObjectStateManager條目。

我按照教程構建了數據庫優先應用程序。目前,我只是從我的組織表中引入數據,直到我能夠順利運作。我的組織模式是這樣的(這是自動產生的「添加代碼生成項目」):

//------------------------------------------------------------------------------ 
// <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; } 
    } 
} 

這是我的組織控制器刪除的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( 「刪除」, 「刪除」,新的{ID = item.OrganizationID})

出於好奇我決定嘗試添加

「@ Html.HiddenFor(型號=> 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%爲什麼 - 有人可以對此事發表看法嗎?

回答

1

當您使用的輔助

@using (Html.BeginForm()) { 

它吐出以下輸出

<form> 

</form> 

,如果你想隱藏字段被張貼你需要得到它的形式裏面,否則它不會被張貼,這是OrganizationID0當你把隱藏的領域之外的形式...