我正在尝试更新表格中的特定行但是我得到了类型' System.Data.Entity.Infrastructure.DbUpdateConcurrencyException'发生在EntityFramework.dll中但未在用户代码中处理
{"存储更新,插入或删除语句影响了意外的行数(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 :(得分:0)
您收到此异常是因为实体框架无法在数据库中找到您的用户对象以更新它READ MORE,因为您正在传递一个只有名字的用户实体,因为我从您的视图中可以看出,您可以做的是,将您的id作为隐藏字段传递,通过id从您的数据库上下文中获取用户模型,设置新用户名,更新,完成。 你就是这样做的
在您的视图中,将ID作为隐藏
传递 @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>
.
.
.
}
控制器中的
// 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();