0

我有一个数据库,我通过实体框架使用它,我需要通过编辑文本框和网格中的其他项目来更新数据,这工作正常,下面是代码:在 aspx.cs

protected void SaveBtn_Click(object sender, EventArgs e)
    {
        obj.Address = AppAddress.Text;
        obj.City = AppCity.Text;
        obj.Email = AppEmail.Text;
        obj.Notes = AppComments.Text;
        obj.Postal = AppPostal.Text;
        obj.AppraiserAppraiserCompanyId = ApprCompCmbx.SelectedIndex;
        obj.ProvinceState = Province.SelectedIndex;
        apprblobj.GetUpdate(obj);
        Response.Write("<script>alert('You have successfully updated the Data');</script>");
}

在 BL 中调用:

 public void GetUpdate(Appraiser appObj)
    {
        obj.UpdateData(appObj);

    }

在 DAL 中

 public void UpdateData(Appraiser apprObj)
    {

        try
        {
            var Appsave = context.Appraisers.FirstOrDefault(App => App.AppraiserId == apprObj.AppraiserId);
            if (Appsave != null)
            {
                Appsave.AppraiserName = apprObj.AppraiserName;
                Appsave.AppraiserAppraiserCompanyId = apprObj.AppraiserAppraiserCompanyId;
                Appsave.Address = apprObj.Address;
                Appsave.City = apprObj.City;
                Appsave.ProvinceState = apprObj.ProvinceState;
                Appsave.Email = apprObj.Email;
                Appsave.Postal = apprObj.Postal;
                Appsave.Notes = apprObj.Notes;

                context.SaveChanges();
            }
        }
        catch (Exception ex)
        {

             log.Debug("Appraiser : UpdateData: " + ex.Message + " Trace : " + ex.StackTrace);
        }
    }

现在我想通过同一个组合框和按钮单击功能添加一个新项目,因为它正在执行所有工作,例如添加、更新和删除。请给我提示:我知道 addobject( _ _) 将用于代替 savechanges 等。

4

0 回答 0