-4

嗨,我使用此查询更新我的数据库,但无法正常工作:

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

2 回答 2

1

尝试将SaveOptions.DetectChangesBeforeSave参数添加到SaveChanges()

dbContext.SaveChanges(SaveOptions.DetectChangesBeforeSave);
于 2012-10-01T06:28:52.527 回答
-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();
                }
            }
        }
于 2012-10-01T06:59:51.387 回答