如何强制EF代码首先查询数据库? [英] How to force EF Code First to query the database?

查看:84
本文介绍了如何强制EF代码首先查询数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们有一个网站管理规则集合和一个单独的Windows窗体应用程序,根据数据库中的规则进行文件级更改。一个EF Code First DbContext的相同的库,但每个应用程序正在实例化自己的上下文副本。



问题是,上下文的每个运行版本都不知道其他版本所做的更改。例如。如果我在网站上更改规则,表单应用程序仍然有以前的版本。



我知道我可能会以这种错误的方式进行,应该有通过JSON / REST从站点到表单应用程序的某种数据访问,但我不希望出于其他原因。



有没有办法禁用缓存上下文并强制每个查询命中DB?

解决方案

没有办法禁用缓存。您必须手动将每个查询设置为reaload数据。该功能不适用于DbContext API =>,您必须使用ObjectContext API。

  ObjectContext objectContext =((IObjectContextAdapter)dbContext).ObjectContext; 
ObjectSet< YourEntity> set = objectContext.CreateObjectSet< YourEntity>();
set.MergeOption = MergeOption.OverwriteChanges;
var query = from x in set where ... select x;

或更简单的场景:如果可能,使用更好的上下文管理,而不是在相同的上下文中运行查询新的。



Btw。在winform应用程序中暴露服务并由网站消费的想法是错误的。您将需要第三个服务应用程序(托管在Web服务器上或作为Windows服务),并且网站和winform应用程序将通过该新应用程序访问数据库。 EF将仅在新的应用程序中。



编辑:



如果您的WinForm应用程序没有进行更改对于从数据库加载的数据,您也可以使用以下命令:

  var query = from x context.YourEntities.AsNoTracking()选择x; 

这将关闭实体的内部变更跟踪,它也应该强制EF每次重新加载实体,这将使保存更改变得更加困难。


I have a site managing a collection of rules and a separate Windows form application making file level changes based on the rules in the database.

Both of these applications use the same libraries for a EF Code First DbContext, but each application is instantiating their own copy of the context.

The problem is, each running version of the context is unaware of the changes made by the other version. E.g. If I change a rule on the site, the forms app still has the previous version.

I'm aware I'm probably going about this wrong way, and should have some kind of data access via JSON/REST from the site to the forms app, but I'd prefer not to for other reasons.

Is there a way to "disable caching" on the context and force each query to hit the DB?

解决方案

No there is no way to disable caching. You must manually set each query to reaload data. That feature is not available with DbContext API => you must use ObjectContext API.

ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
ObjectSet<YourEntity> set = objectContext.CreateObjectSet<YourEntity>();
set.MergeOption = MergeOption.OverwriteChanges;
var query = from x in set where ... select x;    

Or simpler scenario: use better context management if possible and instead of running queries on the same context us a new one.

Btw. idea of having service exposed in winform application and consumed it by web site is wrong. You would need third service application (either hosted on web server or as windows service) and both website and winform application will access database through that new application. EF would be only in the new application.

Edit:

If your WinForm application doesn't make changes to data loaded from database you can also use this:

var query = from x context.YourEntities.AsNoTracking() where ... select x;

This will turn off internal change tracking of entities and it should also force EF to reload entity each time but it will make saving changes much harder.

这篇关于如何强制EF代码首先查询数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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