不先查询就更新记录? [英] Update a record without first querying?

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

问题描述

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

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?

推荐答案

您应该使用 Attach() 方法.

附加和分离对象

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

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