ADO.Entity 更新一条记录的单个字段而不检索整个记录
使用 ADO.Entity 更新一条记录(具有特定 ID)的单个字段的最佳实践是什么? 据我所知,您必须通过 id 检索整个对象,更新属性并调用 SaveChanges:
int id = ...;
var db = new MyEntities();
var o = (from mo in db.myObject
where mo.id = idObject
select mo).First();
o.MyProperty = "some value";
db.SaveChanges();
但检索整个对象似乎有点开销,因为我不关心记录的值,因为我只想设置一个属性,而不考虑值。
另一种选择是为此目的创建一个存储过程......
What is the best practise for updating a single field for one record (with specific ID) using ADO.Entity?
As far as I know, you have to retrieve the whole object by id, update the property and call SaveChanges:
int id = ...;
var db = new MyEntities();
var o = (from mo in db.myObject
where mo.id = idObject
select mo).First();
o.MyProperty = "some value";
db.SaveChanges();
But it seems a little bit overhead having to retrieve the whole object, since I don't care for the values of the record because I just want to set a property, regardless of the values.
Another option would be to create a stored procedure for this purpose...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回复:
使用 ADO.Entity 更新一条记录(具有特定 ID)的单个字段的最佳实践是什么?
答案:最佳实践是检索整个记录,更新一个或多个字段,然后然后存储记录。 ——正如你所做的那样。
Re:
What is the best practise for updating a single field for one record (with specific ID) using ADO.Entity?
Answer: Best practice is to retrieve the entire record, update one or more fields, and then store the record. -- Just as you're doing.