如何更新没有往返机构的实体? (EF 4) [英] How to update an entity without a round-trip? (EF 4)
本文介绍了如何更新没有往返机构的实体? (EF 4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了以下内容:
public void UpdatePlayer(int id)
{
Player player = new Player() {ID = id};
player.Password = "12";
Entities.Players.Attach(player);
Entities.SaveChanges();
}
在db上没有更改。
我是什么缺少?
No change at the db.
What am I missing?
推荐答案
我认为这可能是因为您在附加对象之前设置值 - 数据上下文不会知道哪些字段已经改变了尝试:
I think it might be because you're setting the values before you attach the object - the data context will not know what fields have changed. Try:
public void UpdatePlayer(int id)
{
Player player = new Player() {ID = id};
Entities.Players.Attach(player);
player.Password = "12";
Entities.SaveChanges();
}
这篇关于如何更新没有往返机构的实体? (EF 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文