我创建了一个实体评估师,并且有方法可以选择值,显示数据等。 现在我想保存数据显示后所做的更改,我有一个名为SAVE的按钮,它将用于保存更改。 我无法获得如何保存此实体的更改?
实体名称是Appraiser,我在DAL,BL中创建了get AppriaserDetails等方法,并在aspx.cs中使用它们。
这是我的代码:
public void UpdateData(Appraiser appId)
{
var Appsave = context.Appraisers.FirstOrDefault(App => App.AppraiserId == appId.AppraiserId);
Appsave.AppraiserName = appId.AppraiserName;
Appsave.AppraiserPhones = appId.AppraiserPhones;
Appsave.AppraiserAppraiserCompanyId = appId.AppraiserAppraiserCompanyId;
Appsave.Address = appId.Address;
Appsave.City = appId.City;
Appsave.ProvinceState = appId.ProvinceState;
Appsave.Email = appId.Email;
context.SaveChanges();
}
答案 0 :(得分:1)
如果你想插入新记录,那么可以使用
MyContext.Appraisers.AddObject(appraiserEntityObject);
MyContext.SaveChanges();
如果有更新
if (appraiserEntityObject.EntityState == EntityState.Detached)
{
// In case of web, we got an existing record back from the browser. That object is not attached to the context yet.
MyContext.Appraisers.Attach(appraiserEntityObject);
MyContext.ObjectStateManager.ChangeObjectState(appraiserEntityObject, EntityState.Modified);
}
MyContext.SaveChanges();
这里MyContext是你的ObjectContext