2017-09-16 25 views
0

我想更新我的表中的特定行,但我得到一個異常的類型「System.Data.Entity.Infrastructure.DbUpdateConcurrencyException」發生在EntityFramework.dll但在用戶代碼中沒有處理我越來越'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException'.....當我試圖更新我的表中的一行

{「商店更新,插入或刪除語句影響的行的意外數字(0)。實體可以有「

我的操作方法是

已被修改或刪除,因爲實體已被加載,請參閱 http://go.microsoft.com/fwlink/?LinkId=472540瞭解和處理樂觀併發異常。
public ActionResult createedit() 
    { 
     int id = Convert.ToInt32(Session["UserID"]); 
     var Certtt = (from cert in db.TBL_UserRegistration where cert.UserLoginID == id select cert).FirstOrDefault(); 


     TBL_UserRegistration u = db.TBL_UserRegistration.Find(Certtt.UserRegistrationID); 
     return View(u); 

    } 
    [HttpPost] 
    public ActionResult createedit(TBL_UserRegistration user, HttpPostedFileBase imageup) 
    { 



      if(imageup != null) 
      { 
       user.UserImage = new byte[imageup.ContentLength]; 
       imageup.InputStream.Read(user.UserImage, 0, imageup.ContentLength); 
      } 
      db.Entry(user).State = EntityState.Modified; 
      db.SaveChanges(); 


     return View(user); 

    } 

我看來

@using (Html.BeginForm("createedit", "UserRegistration", FormMethod.Post, new { enctype = "multipart/form-data" })) 
     { 
      @Html.AntiForgeryToken() 

      <form class="wiwet-checkout"> 
       <div class="row"> 
        <div class="col-sm-6"> 
         <!-- <input type="text" placeholder="First Name" />--> 
         @*@Html.LabelFor(model => model.UserFirstName, htmlAttributes: new { @class = "text-label", @placeholder = "Password" })*@ 
         @Html.EditorFor(model => model.UserFirstName, new { htmlAttributes = new { @class = "form-control", @placeholder = "First Name" } }) 
         @Html.ValidationMessageFor(model => model.UserFirstName, "") 
        </div> 
. 
. 
. 
} 

我的模型TBL_UserResgistration是

public partial class TBL_UserRegistration 
{ 
    public TBL_UserRegistration() 
    { 
     this.TBL_Payment = new HashSet<TBL_Payment>(); 
    } 

    public int UserRegistrationID { get; set; } 
    public Nullable<System.DateTime> UserRegistrationDate { get; set; } 
    public string Username { get; set; } 
    public string UserPassword { get; set; } 
    public string UserFirstName { get; set; } 
    public string UserLastName { get; set; } 
    public string UserEmail { get; set; } 
    public string UserType { get; set; } 
    public string UserCity { get; set; } 
    public string UserState { get; set; } 
    public string UserCountry { get; set; } 
    public Nullable<int> UserZip { get; set; } 
    public string UserAddressLine1 { get; set; } 
    public string UserAdressLine2 { get; set; } 
    public Nullable<long> UserPhone1 { get; set; } 
    public Nullable<long> UserPhone2 { get; set; } 
    public byte[] UserImage { get; set; } 
    public Nullable<int> UserLoginID { get; set; } 

    public virtual TBL_Login TBL_Login { get; set; } 
    public virtual ICollection<TBL_Payment> TBL_Payment { get; set; } 
} 
+0

可以添加您的TBL_UserRegistration模型隱藏

@using (Html.BeginForm("createedit", "UserRegistration", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() <!-- here is the hidden field keeping your id --> @Html.Hidden("UserRegistrationID ",Model.UserRegistrationID) <form class="wiwet-checkout"> <div class="row"> <div class="col-sm-6"> <!-- <input type="text" placeholder="First Name" />--> @*@Html.LabelFor(model => model.UserFirstName, htmlAttributes: new { @class = "text-label", @placeholder = "Password" })*@ @Html.EditorFor(model => model.UserFirstName, new { htmlAttributes = new { @class = "form-control", @placeholder = "First Name" } }) @Html.ValidationMessageFor(model => model.UserFirstName, "") </div> . . . } 

? – Munzer

+0

是的只是片刻 – PRADEEP

+0

@Munzer先生請看看我剛纔編輯的帖子 – PRADEEP

回答

0

您收到此異常,因爲實體框架無法在數據庫中找到你的用戶對象來更新它READ MORE,這是因爲您從視圖中可以看到只傳遞了名字的用戶實體,所以您可以做的是,將您的ID作爲隱藏字段傳遞,通過ID從您的數據庫上下文獲取用戶模型,設置新的我們呃名字,更新,完成。 你這是怎麼做到這一點

在您看來,通過ID在你的控制器

// get the real entity from the database using the id passed to your controller 
// as i mentioned before you should keep it in a hidden field 
TBL_UserRegistration u = db.TBL_UserRegistration.Find(user.UserRegistrationID); 
// update the entity user name according to the new one passed from your view 
u.UserFirstName = user.UserFirstName; 
// update and save 
db.Entry(u).State = EntityState.Modified; 
db.SaveChanges(); 
+0

謝謝先生,它爲我工作 – PRADEEP

+0

控制器沒有任何改變...任何方式感謝很多:) – PRADEEP

+0

偉大的,很高興我幫助 – Munzer