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

查看:136
本文介绍了如何在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 Entity框架,并更新它。我很抱歉,如果我在这里缺少一些东西,但这是我一直在做的事情,想知道是否有一个简单的更新方法。谢谢。

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.

旁注:记住要检查你的函数中的null。如果您知道 FirstOrDefault 的返回值永远不会为null,那么您应该使用 First 。您可能还需要考虑使用单个而不是 First

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天全站免登陆