如何更新记录而不ADO.NET实体框架再次选择该记录? [英] How to update a record without selecting that record again in ADO.NET Entity Framework?

查看:113
本文介绍了如何更新记录而不ADO.NET实体框架再次选择该记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我做这样的事情 -

Hi all I am doing something like this -

void update(ClasstoUpdate obj)//obj is already having values to update...
{
  var data= (from i in Entityobject.ClasstoUpdate 
            where obj.Id==i.Id
            select i).FirstorDefault();
  data.Name="SomeCoolName";
  EntityObject.SaveChanges();
}

我想不用再次查询使用ID进行更新,有什么办法我刚更新的对象传递给ADO.NET实体框架,它更新它。我很抱歉,如果我失去了一些东西,但是这是路上我一直在做它不知道是否有更新的简单方法。谢谢你。

I want to perform an update without again querying using the Id,is there any way I just pass the updated object to ADO.NET Entity framework and it updates it.I am sorry if I am missing something here but this is the way i have been doing it wondering if there is a simple way to update. Thanks.

推荐答案

更​​新对象最简单的方法就是把它拿来,改变它,并提交变化,这是你已经在做的事情。

The simple way to update an object is fetch it, change it, and submit changes which is what you're already doing.

另一种方法是附加对象,并告诉该实体框架,该对象是在修改状态。

Another way is to attach the object, and tell the entity framework that the object is in a modified state.

第三种方式是通过构建一个直接在数据库中更新的对象,而不取它的SQL字符串更新对象。不过我不建议这样做。

A third way is to update the object by constructing an SQL string that updates the object directly in the database without fetching it. However I wouldn't recommend doing this.

一个侧面说明:记得在你的函数来检查空。如果你知道 FirstOrDefault 的返回值永远不会为空,那么你应该使用首先代替。您可能还需要考虑使用而不是首先

A side note: remember to check for null in your function. If you know the return value of FirstOrDefault will never be null then you should use First instead. You might also want to consider using Single instead of First.

这篇关于如何更新记录而不ADO.NET实体框架再次选择该记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆