刷新ObjectContext或重新创建它以反映对数据库所做的更改? [英] Refresh ObjectContext or recreate it to reflect changes made to the database?

查看:161
本文介绍了刷新ObjectContext或重新创建它以反映对数据库所做的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务,用于向数据库显示操作。我正在使用实体框架4的DAL。

I have a web service that exposes operations to and from a database. I'm using Entity Framework 4 for the DAL.

我想知道什么是处理和从数据库操作的最佳方式。
最好不要保留ObjectContext的实例,而是为数据库中的每个操作创建一个?这似乎是一个非常糟糕的处理方式,但我不能想到另一种方式来跟上对数据库所做的更改。

I'm wondering what would be the best way to handle operations to and from the database. Would it be best not to keep alive an instance of the ObjectContext and instead create one for each operation done to the database? This seems to be a very bad way to handle this, but i can't think of another way to keep up with the changes made to the database.

如果我保持相同的上下文活着,数据如何被修改会怎么知道?我明白当查询数据库时,ObjectContext只是检查记录是否被添加或删除,通过检查主键字段(我是否正确?)。在这种情况下,我必须明确地告诉它刷新一个特定的对象,这不是很实用。

If i keep the same context alive, how will it know when data has been modified? I understand that when querying the database, the ObjectContext just checks if records have been added or deleted, by checking the Primary Key fields (am i correct? ). In that case, i have to explicitly tell it to refresh a particular object, which is not very practical.

我在想一个使用时间戳列的解决方案,并且objectcontext检查该列以进行更改。如果该列被修改,它将刷新数据。
这样的一个解决方案是建立在Entity Framework 4中?或者有人有任何其他建议,我可以随时从数据库获取最新的数据,而不会为每个请求重新创建ObjectContext?

I'm thinking of a solution where a timestamp column is used, and the objectcontext checks that column for changes. If that column is modified, it refreshes the data. Is such a solution built inside Entity Framework 4? Or does anyone have any other suggestions on how i can always get the latest data from the database without recreating the ObjectContext for each request?

推荐答案

p> ObjectContext旨在用作工作单元,因此您可以为每个业务交易创建一个,然后处理它。

The ObjectContext is meant to be used as a Unit of work, so you create one for every business transaction and dispose of it afterwards.

EF - 和存储库模式 - 多个上下文

这篇关于刷新ObjectContext或重新创建它以反映对数据库所做的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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