要修改的对象是从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。
Previous: 06节-使用EF修改一条记录
Next: 08节-使用EF删除记录