嗨我用这个查询更新我的数据库但没有工作:
if (Request["mode"] != null)
{
Int32 ID = Int32.Parse(Request["Id"].ToString());
using (NoavaranModel.NoavaranEntities1 dbContext = new NoavaranModel.NoavaranEntities1())
{
var ToEdit = (from n in dbContext.Packages
where n.Id == ID
select n).FirstOrDefault();
ToEdit.Name = txtName.Text;
ToEdit.Level = txtLevel.Text;
ToEdit.Description = txtDescription.Text;
ToEdit.ForAge = ddlAgeFor.SelectedItem.Text;
ToEdit.CatId = ddlCategory.SelectedIndex + 1;
dbContext.SaveChanges();
//Response.Redirect("Packages.aspx");
//return;*/
}
}
答案 0 :(得分:1)
尝试将SaveOptions.DetectChangesBeforeSave
参数添加到SaveChanges()
:
dbContext.SaveChanges(SaveOptions.DetectChangesBeforeSave);
答案 1 :(得分:-1)
我解决了我的问题,我忘记了我使用的是IsPostBack:
if (!IsPostBack)
{
if (Request["Id"] != null)
{
Int32 ID = Int32.Parse(Request["Id"].ToString());
using (NoavaranModel.NoavaranEntities1 dbContext = new NoavaranModel.NoavaranEntities1())
{
var query = (from list in dbContext.Packages
where list.Id == ID
select list).FirstOrDefault();
txtName.Text = query.Name;
txtLevel.Text = query.Level;
txtDescription.Text = query.Description;
Image2.ImageUrl = "PackageImages/" + query.Image;
ddlAgeFor.Text = query.ForAge;
//ddlCategory.Text = query.CatId.ToString();
}
}
}