更新记录没有第一次查询? [英] Update a record without first querying?

查看:106
本文介绍了更新记录没有第一次查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我查询数据库并加载项目列表。然后我以详细视图形式打开其中一个项目,而不是从数据库中重新查询该项目,而是从列表中的数据源创建一个项目实例。



有没有办法可以更新数据库记录,而无需获取单个项目的记录?



以下是我现在正在做的一个示例:

  dataItem itemToUpdate =(from t in dataEntity.items 
其中t.id == id
select吨).FirstOrDefault();

然后拉出记录后,我更新项目中的某些值并将记录推回:

  itemToUpdate.itemstatus = newStatus; 
dataEntity.SaveChanges();

我会认为会有更好的方法来做到这一点吗?

解决方案

您应该使用 Attach()方法。



附加和分离对象


Lets say I query the database and load a list of items. Then I open one of the items in a detail view form, and instead of re-querying the item out of the database, I create an instance of the item from the datasource in the list.

Is there a way I can update the database record without fetching the record of the individual item?

Here is a sample how I am doing it now:

dataItem itemToUpdate = (from t in dataEntity.items
                                 where t.id == id
                                 select t).FirstOrDefault();

Then after pulling the record I update some values in the item and push the record back:

itemToUpdate.itemstatus = newStatus;
dataEntity.SaveChanges();

I would think there would be a better way to do this, any ideas?

解决方案

You should use the Attach() method.

Attaching and Detaching Objects

这篇关于更新记录没有第一次查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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