我有一种看起来像这样的方法:
// Adds an element to the TableElement table in SQL
AddToSql(TableElement deserializedObject)
{
MyEntities entities = new MyEntities();
entities.TableElement.AddObject(deserializedObject);
entities.SaveChanges();
}
我尝试编写类似的方法来更新现有行,但我的方法不起作用
// Doesn't work
UpdateSql(TableElement deserializedObject)
{
MyEntities entities = new MyEntities();
int id = deserializedObject.id;
TableElement elementToOverwrite = entities.TableElement
.Single(s => s.Id == deserializedObject.Id);
// Somehow, I want to give "elementToOverwrite" all the values in "deserializedObject"
// I NEED TO KNOW WHAT GOES HERE.
entities.SaveChanges();
}
更新:看起来我可以使用entities.ApplyCurrentValues("TableElement", deserializedObject)
,但如果我的反序列化对象包含其他表中的相关行,则不会受到影响。