EF6如何写update方法

cnuusw 2014-04-15 10:35:19

public void UpdateWithLog(T entity, Guid creatorGuid)
{
dbContext.Entry(entity).State = EntityState.Modified;
//dbContext.Set<T>().Attach(entity);
//dbContext.Entry(entity).State = EntityState.Modified;
dbContext.Set<ModelLog>().Add(GetModelLog(entity, creatorGuid, "Update"));
}



如上方法报错:




具体操作过程:
Find----序列化-----反序列化-----修改model值----update

求助,该如何写EF的Update方法。



补充:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request.QueryString["userid"]) && CheckValue.IsInteger(Request.QueryString["userid"]))
{
LoadInfo();
}
else
{
this.btnSave.Enabled = false;
}
}
}

protected void btnSave_Click(object sender, EventArgs e)
{
BLL.UserService bll = new BLL.UserService();
Model.User model = (Model.User)ViewState["model"];
//Model.User model = bll.GetById(int.Parse(Request.QueryString["userid"]));
model.OrganizationID = int.Parse(this.OrganizationList.SelectedValue);
bll.UpdateWithLog(model);
ShowMsgHelper.ShowScript("art.dialog({title: \"恭喜,操作成功\", content: \"恭喜,操作成功,用户信息已成功更新!\", ok:function(){ art.dialog.close(); art.dialog.opener.ListGrid(); }});");
}


private void LoadInfo()
{
BLL.UserService bll = new BLL.UserService();
BLL.UserTypeService usertype = new BLL.UserTypeService();
BLL.OrganizationService org = new BLL.OrganizationService();
Model.User model = bll.GetById(int.Parse(Request.QueryString["userid"]));

CSS = model.Sex ? "man" : "woman";
OfficeID = model.OfficeID;
this.ltlUserName.Text = model.UserName;
this.ltlRealName.Text = model.RealName;
this.ltlUserType.Text = usertype.GetById(model.UserTypeID).UserTypeName;
this.ltlOrganizationName.Text = org.GetOrganizationFullName(model.OrganizationID);
ViewState["model"] = model;

this.OrganizationList.SelectedValue = model.OrganizationID.ToString();
}




使用viewstate后就出该问题,如果不使用viewstate 而是在此getbyid就无问题。 求解。
...全文
180 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧