我可以更新EF实体,而不首先查询它吗? [英] Can I update an EF Entity without querying for it first?

查看:86
本文介绍了我可以更新EF实体,而不首先查询它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的方案:


  • 我有一个通过WCF传递的简单消息。此消息表示现有的数据库记录,并且具有执行计算所需的数据字段的所有。

  • 完成计算后,我要更新

目前,尝试简单地设置Entity框架的所有字段实体的版本,然后坚持对象上下文的状态似乎什么都不做。

Currently, attempting to simply set all of the fields of the Entity framework's version of the entity and then persisting the object context's state seems to do nothing.

任何想法?

推荐答案

是的,你可以。该过程是使一个类似于实体的原始状态的实体,将其附加到ObjectContext,修改实体,然后将其保存到SaveChanges。记住,重要的是将实体附加到之前的ObjectContext 中,您修改实体。有一个例子在这篇文章

Yes you can. The procedure is to make an entity which resembles the original state of the entity, attach it to the ObjectContext, modify the entity, then SaveChanges. Remember, it is important to attach the entity to the ObjectContext before you modify the entity. There is an example in this post.

这篇关于我可以更新EF实体,而不首先查询它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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