为什么我的查询无法正常工作?

时间:2012-10-01 06:19:56

标签: asp.net entity-framework ef-database-first

嗨我用这个查询更新我的数据库但没有工作:

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

2 个答案:

答案 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();
                }
            }
        }