07节-修改记录的时候,使用附加的方式

要修改的对象是从session中得到:

csharpcsharpAccount currentLoginAccount = Session["currentaccount"] as WebApplicationCF01.Models.Account;

然后我们直接修改phone属性,直接SaveChanges。

结果我们发现,修改这个currentLoginAccount会把session里面的值也一并修改,但是数据库中的记录是没改变的。

我们将实体附加到上下文中:

csharpcsharpcontext.account.Attach(currentLoginAccount);

然后执行,还是不能修改的。那么继续将修改状态改为Modified:

csharpcsharpcontext.Entry<Account>(currentLoginAccount).State = System.Data.Entity.EntityState.Modified;

其实这里关键应该是这个ID。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注