我尝试了以下方法:
public void UpdatePlayer(int id) { Player player = new Player() {ID = id}; player.Password = "12"; Entities.Players.Attach(player); Entities.SaveChanges(); }
数据库没有变化.
我错过了什么?
解决方法
我认为这可能是因为您在附加对象之前设置了值 – 数据上下文将不知道哪些字段已更改.尝试:
public void UpdatePlayer(int id) { Player player = new Player() {ID = id}; Entities.Players.Attach(player); player.Password = "12"; Entities.SaveChanges(); }